1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
|
/* Copyright (C) 2001-2006 Artifex Software, Inc.
All Rights Reserved.
This software is provided AS-IS with no warranty, either express or
implied.
This software is distributed under license and may not be copied, modified
or distributed except as expressly authorized under the terms of that
license. Refer to licensing information at http://www.artifex.com/
or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.
*/
/* $Id: iinit.c 9043 2008-08-28 22:48:19Z giles $ */
/* Initialize internally known objects for Ghostscript interpreter */
#include "string_.h"
#include "ghost.h"
#include "gscdefs.h"
#include "gsexit.h"
#include "gsstruct.h"
#include "ierrors.h"
#include "ialloc.h"
#include "iddict.h"
#include "dstack.h"
#include "ilevel.h"
#include "iinit.h"
#include "iname.h"
#include "interp.h"
#include "ipacked.h"
#include "iparray.h"
#include "iutil.h"
#include "ivmspace.h"
#include "opdef.h"
#include "store.h"
/* Implementation parameters. */
/*
* Define the (initial) sizes of the various system dictionaries. We want
* the sizes to be prime numbers large enough to cover all the operators,
* plus everything in the init files, even if all the optional features are
* selected. Note that these sizes must be large enough to get us through
* initialization, since we start up in Level 1 mode where dictionaries
* don't expand automatically.
*/
/* The size of systemdict can be set in the makefile. */
#ifndef SYSTEMDICT_SIZE
# define SYSTEMDICT_SIZE 631
#endif
#ifndef SYSTEMDICT_LEVEL2_SIZE
# define SYSTEMDICT_LEVEL2_SIZE 983
#endif
#ifndef SYSTEMDICT_LL3_SIZE
# define SYSTEMDICT_LL3_SIZE 1123
#endif
/* The size of level2dict, if applicable, can be set in the makefile. */
#ifndef LEVEL2DICT_SIZE
# define LEVEL2DICT_SIZE 251
#endif
/* Ditto the size of ll3dict. */
#ifndef LL3DICT_SIZE
# define LL3DICT_SIZE 43
#endif
/* Ditto the size of filterdict. */
#ifndef FILTERDICT_SIZE
# define FILTERDICT_SIZE 43
#endif
/* Define an arbitrary size for the operator procedure tables. */
#ifndef OP_ARRAY_TABLE_SIZE
# define OP_ARRAY_TABLE_SIZE 300
#endif
#ifndef OP_ARRAY_TABLE_GLOBAL_SIZE
# define OP_ARRAY_TABLE_GLOBAL_SIZE OP_ARRAY_TABLE_SIZE
#endif
#ifndef OP_ARRAY_TABLE_LOCAL_SIZE
# define OP_ARRAY_TABLE_LOCAL_SIZE (OP_ARRAY_TABLE_SIZE / 2)
#endif
#define OP_ARRAY_TABLE_TOTAL_SIZE\
(OP_ARRAY_TABLE_GLOBAL_SIZE + OP_ARRAY_TABLE_LOCAL_SIZE)
/* Define the list of error names. */
const char *const gs_error_names[] =
{
ERROR_NAMES
};
/* The operator tables */
op_array_table op_array_table_global, op_array_table_local; /* definitions of `operator' procedures */
/* Enter a name and value into a dictionary. */
static int
i_initial_enter_name_in(i_ctx_t *i_ctx_p, ref *pdict, const char *nstr,
const ref * pref)
{
int code = idict_put_string(pdict, nstr, pref);
if (code < 0)
lprintf4("initial_enter failed (%d), entering /%s in -dict:%u/%u-\n",
code, nstr, dict_length(pdict), dict_maxlength(pdict));
return code;
}
int
i_initial_enter_name(i_ctx_t *i_ctx_p, const char *nstr, const ref * pref)
{
return i_initial_enter_name_in(i_ctx_p, systemdict, nstr, pref);
}
/* Remove a name from systemdict. */
void
i_initial_remove_name(i_ctx_t *i_ctx_p, const char *nstr)
{
ref nref;
if (name_ref(imemory, (const byte *)nstr, strlen(nstr), &nref, -1) >= 0)
idict_undef(systemdict, &nref);
}
/* Define the names and sizes of the initial dictionaries. */
/* The names are used to create references in systemdict. */
const struct {
const char *name;
uint size;
bool local;
} initial_dictionaries[] = {
#ifdef INITIAL_DICTIONARIES
INITIAL_DICTIONARIES
#else
/* systemdict is created and named automagically */
{
"level2dict", LEVEL2DICT_SIZE, false
},
{
"ll3dict", LL3DICT_SIZE, false
},
{
"globaldict", 0, false
},
{
"userdict", 0, true
},
{
"filterdict", FILTERDICT_SIZE, false
},
#endif
};
/* systemdict and globaldict are magically inserted at the bottom */
const char *const initial_dstack[] =
{
#ifdef INITIAL_DSTACK
INITIAL_DSTACK
#else
"userdict"
#endif
};
#define MIN_DSTACK_SIZE (countof(initial_dstack) + 1) /* +1 for systemdict */
/*
* Detect whether we have any Level 2 or LanguageLevel 3 operators.
* We export this for gs_init1 in imain.c.
* This is slow, but we only call it a couple of times.
*/
static int
gs_op_language_level(void)
{
const op_def *const *tptr;
int level = 1;
for (tptr = op_defs_all; *tptr != 0; ++tptr) {
const op_def *def;
for (def = *tptr; def->oname != 0; ++def)
if (op_def_is_begin_dict(def)) {
if (!strcmp(def->oname, "level2dict"))
level = max(level, 2);
else if (!strcmp(def->oname, "ll3dict"))
level = max(level, 3);
}
}
return level;
}
bool
gs_have_level2(void)
{
return (gs_op_language_level() >= 2);
}
/* Create an initial dictionary if necessary. */
static ref *
make_initial_dict(i_ctx_t *i_ctx_p, const char *iname, ref idicts[])
{
int i;
/* systemdict was created specially. */
if (!strcmp(iname, "systemdict"))
return systemdict;
for (i = 0; i < countof(initial_dictionaries); i++) {
const char *dname = initial_dictionaries[i].name;
const int dsize = initial_dictionaries[i].size;
if (!strcmp(iname, dname)) {
ref *dref = &idicts[i];
if (r_has_type(dref, t_null)) {
gs_ref_memory_t *mem =
(initial_dictionaries[i].local ?
iimemory_local : iimemory_global);
int code = dict_alloc(mem, dsize, dref);
if (code < 0)
return 0; /* disaster */
}
return dref;
}
}
/*
* Name mentioned in some op_def, but not in initial_dictionaries.
* Punt.
*/
return 0;
}
/* Initialize objects other than operators. In particular, */
/* initialize the dictionaries that hold operator definitions. */
int
obj_init(i_ctx_t **pi_ctx_p, gs_dual_memory_t *idmem)
{
int level = gs_op_language_level();
ref system_dict;
i_ctx_t *i_ctx_p;
int code;
/*
* Create systemdict. The context machinery requires that
* we do this before initializing the interpreter.
*/
code = dict_alloc(idmem->space_global,
(level >= 3 ? SYSTEMDICT_LL3_SIZE :
level >= 2 ? SYSTEMDICT_LEVEL2_SIZE : SYSTEMDICT_SIZE),
&system_dict);
if (code < 0)
return code;
/* Initialize the interpreter. */
code = gs_interp_init(pi_ctx_p, &system_dict, idmem);
if (code < 0)
return code;
i_ctx_p = *pi_ctx_p;
{
#define icount countof(initial_dictionaries)
ref idicts[icount];
int i;
const op_def *const *tptr;
min_dstack_size = MIN_DSTACK_SIZE;
refset_null(idicts, icount);
/* Put systemdict on the dictionary stack. */
if (level >= 2) {
dsp += 2;
/*
* For the moment, let globaldict be an alias for systemdict.
*/
dsp[-1] = system_dict;
min_dstack_size++;
} else {
++dsp;
}
*dsp = system_dict;
/* Create dictionaries which are to be homes for operators. */
for (tptr = op_defs_all; *tptr != 0; tptr++) {
const op_def *def;
for (def = *tptr; def->oname != 0; def++)
if (op_def_is_begin_dict(def)) {
if (make_initial_dict(i_ctx_p, def->oname, idicts) == 0)
return_error(e_VMerror);
}
}
/* Set up the initial dstack. */
for (i = 0; i < countof(initial_dstack); i++) {
const char *dname = initial_dstack[i];
++dsp;
if (!strcmp(dname, "userdict"))
dstack_userdict_index = dsp - dsbot;
ref_assign(dsp, make_initial_dict(i_ctx_p, dname, idicts));
}
/* Enter names of referenced initial dictionaries into systemdict. */
initial_enter_name("systemdict", systemdict);
for (i = 0; i < icount; i++) {
ref *idict = &idicts[i];
if (!r_has_type(idict, t_null)) {
/*
* Note that we enter the dictionary in systemdict
* even if it is in local VM. There is a special
* provision in the garbage collector for this:
* see ivmspace.h for more information.
* In order to do this, we must temporarily
* identify systemdict as local, so that the
* store check in dict_put won't fail.
*/
uint save_space = r_space(systemdict);
r_set_space(systemdict, avm_local);
code = initial_enter_name(initial_dictionaries[i].name,
idict);
r_set_space(systemdict, save_space);
if (code < 0)
return code;
}
}
#undef icount
}
gs_interp_reset(i_ctx_p);
{
ref vnull, vtrue, vfalse;
make_null(&vnull);
make_true(&vtrue);
make_false(&vfalse);
if ((code = initial_enter_name("null", &vnull)) < 0 ||
(code = initial_enter_name("true", &vtrue)) < 0 ||
(code = initial_enter_name("false", &vfalse)) < 0
)
return code;
}
/* Create the error name table */
{
int n = countof(gs_error_names) - 1;
int i;
ref era;
code = ialloc_ref_array(&era, a_readonly, n, "ErrorNames");
if (code < 0)
return code;
for (i = 0; i < n; i++)
if ((code = name_enter_string(imemory, (const char *)gs_error_names[i],
era.value.refs + i)) < 0)
return code;
return initial_enter_name("ErrorNames", &era);
}
}
/* Run the initialization procedures of the individual operator files. */
int
zop_init(i_ctx_t *i_ctx_p)
{
const op_def *const *tptr;
int code;
/* Because of a bug in Sun's SC1.0 compiler, */
/* we have to spell out the typedef for op_def_ptr here: */
const op_def *def;
for (tptr = op_defs_all; *tptr != 0; tptr++) {
for (def = *tptr; def->oname != 0; def++)
DO_NOTHING;
if (def->proc != 0) {
code = def->proc(i_ctx_p);
if (code < 0) {
lprintf2("op_init proc 0x%lx returned error %d!\n",
(ulong)def->proc, code);
return code;
}
}
}
/* Initialize the predefined names other than operators. */
/* Do this here in case op_init changed any of them. */
{
ref vcr, vpr, vpf, vre, vrd;
make_const_string(&vcr, a_readonly | avm_foreign,
strlen(gs_copyright), (const byte *)gs_copyright);
make_const_string(&vpr, a_readonly | avm_foreign,
strlen(gs_product), (const byte *)gs_product);
make_const_string(&vpf, a_readonly | avm_foreign,
strlen(gs_productfamily),
(const byte *)gs_productfamily);
make_int(&vre, gs_revision);
make_int(&vrd, gs_revisiondate);
if ((code = initial_enter_name("copyright", &vcr)) < 0 ||
(code = initial_enter_name("product", &vpr)) < 0 ||
(code = initial_enter_name("productfamily", &vpf)) < 0 ||
(code = initial_enter_name("revision", &vre)) < 0 ||
(code = initial_enter_name("revisiondate", &vrd)) < 0)
return code;
}
return 0;
}
/* Create an op_array table. */
static int
alloc_op_array_table(i_ctx_t *i_ctx_p, uint size, uint space,
op_array_table *opt)
{
uint save_space = ialloc_space(idmemory);
int code;
ialloc_set_space(idmemory, space);
code = ialloc_ref_array(&opt->table, a_readonly, size,
"op_array table");
ialloc_set_space(idmemory, save_space);
if (code < 0)
return code;
refset_null(opt->table.value.refs, size);
opt->nx_table =
(ushort *) ialloc_byte_array(size, sizeof(ushort),
"op_array nx_table");
if (opt->nx_table == 0)
return_error(e_VMerror);
opt->count = 0;
opt->root_p = &opt->table;
opt->attrs = space | a_executable;
return 0;
}
/* Initialize the operator table. */
int
op_init(i_ctx_t *i_ctx_p)
{
const op_def *const *tptr;
int code;
/* Enter each operator into the appropriate dictionary. */
for (tptr = op_defs_all; *tptr != 0; tptr++) {
ref *pdict = systemdict;
const op_def *def;
const char *nstr;
for (def = *tptr; (nstr = def->oname) != 0; def++)
if (op_def_is_begin_dict(def)) {
ref nref;
code = name_ref(imemory, (const byte *)nstr, strlen(nstr), &nref, -1);
if (code < 0)
return code;
if (!dict_find(systemdict, &nref, &pdict))
return_error(e_Fatal);
if (!r_has_type(pdict, t_dictionary))
return_error(e_Fatal);
} else {
ref oper;
uint index_in_table = def - *tptr;
uint opidx = (tptr - op_defs_all) * OP_DEFS_MAX_SIZE +
index_in_table;
if (index_in_table >= OP_DEFS_MAX_SIZE) {
lprintf1("opdef overrun! %s\n", def->oname);
return_error(e_Fatal);
}
gs_interp_make_oper(&oper, def->proc, opidx);
/* The first character of the name is a digit */
/* giving the minimum acceptable number of operands. */
/* Check to make sure it's within bounds. */
if (*nstr - '0' > gs_interp_max_op_num_args)
return_error(e_Fatal);
nstr++;
/*
* Skip internal operators, and the second occurrence of
* operators with special indices.
*/
if (*nstr != '%' && r_size(&oper) == opidx) {
code =
i_initial_enter_name_in(i_ctx_p, pdict, nstr, &oper);
if (code < 0)
return code;
}
}
}
/* Allocate the tables for `operator' procedures. */
/* Make one of them local so we can have local operators. */
if ((code = alloc_op_array_table(i_ctx_p, OP_ARRAY_TABLE_GLOBAL_SIZE,
avm_global, &op_array_table_global) < 0))
return code;
op_array_table_global.base_index = op_def_count;
if ((code = gs_register_ref_root(imemory, NULL,
(void **)&op_array_table_global.root_p,
"op_array_table(global)")) < 0 ||
(code = gs_register_struct_root(imemory, NULL,
(void **)&op_array_table_global.nx_table,
"op_array nx_table(global)")) < 0 ||
(code = alloc_op_array_table(i_ctx_p, OP_ARRAY_TABLE_LOCAL_SIZE,
avm_local, &op_array_table_local) < 0)
)
return code;
op_array_table_local.base_index =
op_array_table_global.base_index +
r_size(&op_array_table_global.table);
if ((code = gs_register_ref_root(imemory, NULL,
(void **)&op_array_table_local.root_p,
"op_array_table(local)")) < 0 ||
(code = gs_register_struct_root(imemory, NULL,
(void **)&op_array_table_local.nx_table,
"op_array nx_table(local)")) < 0
)
return code;
return 0;
}
#ifdef DEBUG_TRACE_PS_OPERATORS
static const char *unknown_op_name = "unknown_op";
const char *
op_get_name_string(op_proc_t opproc)
{
const op_def *const *tptr;
int code;
for (tptr = op_defs_all; *tptr != 0; tptr++) {
const op_def *def;
for (def = *tptr; def->oname != 0; def++)
if (!op_def_is_begin_dict(def)) {
if (def->proc == opproc)
return def->oname;
}
}
return unknown_op_name;
}
#endif
|