home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / IINIT.C < prev    next >
C/C++ Source or Header  |  1994-08-01  |  14KB  |  457 lines

  1. /* Copyright (C) 1989, 1992, 1993, 1994 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* iinit.c */
  20. /* Initialize internally known objects for Ghostscript interpreter */
  21. #include "string_.h"
  22. #include "ghost.h"
  23. #include "gsexit.h"
  24. #include "gsstruct.h"
  25. #define INCLUDE_ERROR_NAMES        /* see errors.h */
  26. #include "errors.h"
  27. #include "ialloc.h"
  28. #include "idict.h"
  29. #include "dstack.h"
  30. #include "ilevel.h"
  31. #include "iname.h"
  32. #include "interp.h"
  33. #include "ipacked.h"
  34. #include "iparray.h"
  35. #include "oper.h"
  36. #include "store.h"
  37.  
  38. /* Define various parameters of this interpreter. */
  39. /* All of these can be set in the makefile. */
  40. #ifndef GS_BUILDTIME
  41. #  define GS_BUILDTIME\
  42.     0        /****** HOW TO SET THIS? ******/
  43. #endif
  44. const long gs_buildtime = GS_BUILDTIME;
  45. #ifndef GS_COPYRIGHT
  46. #  define GS_COPYRIGHT\
  47.     "Copyright (C) 1990, 91, 92, 93, 94 Aladdin Enterprises, Menlo Park, CA.\n"
  48. #endif
  49. const char *gs_copyright = GS_COPYRIGHT;    
  50. #ifndef GS_PRODUCT
  51. #  define GS_PRODUCT\
  52.     "Aladdin Ghostscript"
  53. #endif
  54. const char *gs_product = GS_PRODUCT;
  55. #ifndef GS_REVISION
  56. #  define GS_REVISION\
  57.     3000        /* primary release # x 1000 + */\
  58.             /* secondary release # x 100 + tertiary release #. */
  59. #endif
  60. const long gs_revision = GS_REVISION;
  61. #ifndef GS_REVISIONDATE
  62. #  define GS_REVISIONDATE\
  63.     19940801    /* year x 10000 + month x 100 + day. */
  64. #endif
  65. const long gs_revisiondate = GS_REVISIONDATE;
  66. #ifndef GS_SERIALNUMBER
  67. #  define GS_SERIALNUMBER\
  68.     42        /* a famous number */
  69. #endif
  70. const long gs_serialnumber = GS_SERIALNUMBER;
  71.  
  72. /* Implementation parameters. */
  73. /* The size of systemdict can be set in the makefile. */
  74. /* We want the sizes to be prime numbers large enough to cover */
  75. /* all the operators, plus everything in the init files, */
  76. /* even if all the optional features are selected. */
  77. #ifndef SYSTEMDICT_SIZE
  78. #  define SYSTEMDICT_SIZE 479
  79. #endif
  80. #ifndef SYSTEMDICT_LEVEL2_SIZE
  81. #  define SYSTEMDICT_LEVEL2_SIZE 631
  82. #endif
  83. /* The size of level2dict, if applicable, can be set in the makefile. */
  84. #ifndef LEVEL2DICT_SIZE
  85. #  define LEVEL2DICT_SIZE 149
  86. #endif
  87. /* Define an arbitrary size for the pseudo-operator table. */
  88. #ifndef OP_ARRAY_TABLE_SIZE
  89. #  define OP_ARRAY_TABLE_SIZE 100
  90. #endif
  91.  
  92. /* The operator tables */
  93. /* Because of a bug in Sun's SC1.0 compiler, */
  94. /* we have to spell out the typedef for op_def_ptr here: */
  95. const op_def _ds **op_def_table;
  96. uint op_def_count;
  97. ref op_array_table;    /* t_array, definitions of `operator' procedures */
  98. ushort *op_array_nx_table;        /* name indices for same */
  99. uint op_array_count;
  100. /* GC roots for the same */
  101. private ref *op_array_p = &op_array_table;
  102. private gs_gc_root_t op_def_root, op_array_root, op_array_nx_root;
  103.  
  104. /* Enter a name and value into a dictionary. */
  105. void
  106. initial_enter_name_in(const char *nstr, const ref *pref, ref *pdict)
  107. {    int code = dict_put_string(pdict, nstr, pref);
  108.     if ( code < 0 )
  109.       {    lprintf2("initial_enter failed (%d) - %s\n", code, nstr),
  110.         gs_exit(1);
  111.       }
  112. }
  113. void
  114. initial_enter_name(const char *nstr, const ref *pref)
  115. {    initial_enter_name_in(nstr, pref, systemdict);
  116. }
  117.  
  118. /* Create a name.  Fatal error if it fails. */
  119. private void
  120. name_enter(const char *str, ref *pref)
  121. {    if ( name_enter_string(str, pref) != 0 )
  122.       {    lprintf1("name_enter failed - %s\n", str);
  123.         gs_exit(1);
  124.       }
  125. }
  126.  
  127. /* Initialize the operators. */
  128. /* Optional operators must come after standard ones, */
  129. /* so they can replace them. */
  130.     /* Non-graphics operators */
  131. extern op_def
  132.   gsmain_op_defs[],
  133.   zarith_op_defs[], zarray_op_defs[], zcontrol_op_defs[], zdict_op_defs[],
  134.   zfile_op_defs[], zfileio_op_defs[], zfproc_op_defs[],
  135.   zfilter_op_defs[], zgeneric_op_defs[], ziodev_op_defs[],
  136.   zmath_op_defs[], zmisc_op_defs[], zpacked_op_defs[],
  137.   zrelbit_op_defs[], zstack_op_defs[], zstring_op_defs[],
  138.   ztoken_op_defs[], ztype_op_defs[], zvmem_op_defs[],
  139.     /* Graphics operators */
  140.   zchar_op_defs[], zcolor_op_defs[], zdevice_op_defs[],
  141.   zfont_op_defs[], zfont2_op_defs[], zgstate_op_defs[], zht_op_defs[],
  142.   zmatrix_op_defs[], zpaint_op_defs[], zpath_op_defs[],
  143.   zpath2_op_defs[],
  144.     /* Optional operators */
  145. #define oper_(defs) defs[],
  146. #include "gconfig.h"
  147. #undef oper_
  148.     /* Interpreter operators */
  149.   interp_op_defs[];
  150. private op_def_ptr op_defs_all[] = {
  151.     /* Non-graphics operators */
  152.   gsmain_op_defs,
  153.   zarith_op_defs, zarray_op_defs, zcontrol_op_defs, zdict_op_defs,
  154.   zfile_op_defs, zfileio_op_defs, zfproc_op_defs,
  155.   zfilter_op_defs, zgeneric_op_defs, ziodev_op_defs,
  156.   zmath_op_defs, zmisc_op_defs, zpacked_op_defs,
  157.   zrelbit_op_defs, zstack_op_defs, zstring_op_defs,
  158.   ztoken_op_defs, ztype_op_defs, zvmem_op_defs,
  159.     /* Graphics operators */
  160.   zchar_op_defs, zcolor_op_defs, zdevice_op_defs,
  161.   zfont_op_defs, zfont2_op_defs, zgstate_op_defs, zht_op_defs,
  162.   zmatrix_op_defs, zpaint_op_defs, zpath_op_defs,
  163.   zpath2_op_defs,
  164.     /* Optional operators */
  165. #define oper_(defs) defs,
  166. #include "gconfig.h"
  167. #undef oper_
  168.     /* Interpreter operators */
  169.   interp_op_defs,
  170.     /* end marker */
  171.   (op_def_ptr)0
  172. };
  173.  
  174. /* Define the names and sizes of the initial dictionaries. */
  175. /* The names are used to create references in systemdict. */
  176. const struct { const char *name; uint size; bool local; }
  177.   initial_dictionaries[] = {
  178. #ifdef INITIAL_DICTIONARIES
  179.     INITIAL_DICTIONARIES
  180. #else
  181.     /* systemdict is created and named automagically */
  182.     { "level2dict", LEVEL2DICT_SIZE, false },
  183.     { "globaldict", 0, false },
  184.     { "userdict", 0, true }
  185. #endif
  186. };
  187. /* systemdict and globaldict are magically inserted at the bottom */
  188. const char *initial_dstack[] = {
  189. #ifdef INITIAL_DSTACK
  190.     INITIAL_DSTACK
  191. #else
  192.     "userdict"
  193. #endif
  194. };
  195. #define MIN_DSTACK_SIZE (countof(initial_dstack) + 1) /* +1 for systemdict */
  196.  
  197.  
  198. /* Detect whether we have any Level 2 operators. */
  199. /* We export this for gs_init1 in gsmain.c. */
  200. bool
  201. gs_have_level2(void)
  202. {    int i;
  203.     for ( i = 0; i < countof(initial_dictionaries); i++ )
  204.       if ( !strcmp(initial_dictionaries[i].name, "level2dict") )
  205.         return true;
  206.     return false;
  207. }
  208.  
  209. /* Create an initial dictionary if necessary. */
  210. private ref *
  211. make_initial_dict(const char *iname, uint namelen, ref idicts[])
  212. {    int i;
  213.     static const char sysname[] = "systemdict";
  214.  
  215.     /*
  216.      * We wait to create systemdict until after everything else,
  217.      * so we can size it according to whether Level 2 is present.
  218.      */
  219.     if ( (namelen == countof(sysname) - 1) &&
  220.          !memcmp(iname, sysname, namelen)
  221.        )
  222.       return 0;
  223.  
  224.     for ( i = 0; i < countof(initial_dictionaries); i++ )
  225.       {    const char *dname = initial_dictionaries[i].name;
  226.         const int dsize = initial_dictionaries[i].size;
  227.  
  228.         if ( (namelen == strlen(dname)) &&
  229.              !memcmp(iname, dname, namelen)
  230.            )
  231.           {    ref *dref = &idicts[i];
  232.             if ( r_has_type(dref, t_null) )
  233.               {    int code;
  234.                 /* Perhaps dict_create should take */
  235.                 /* the allocator as an argument.... */
  236.                 bool local = ialloc_is_local(idmemory);
  237.                 ialloc_set_local(idmemory,
  238.                          initial_dictionaries[i].local);
  239.                 code = dict_create(dsize, dref);
  240.                 ialloc_set_local(idmemory, local);
  241.                 if ( code < 0 ) abort();
  242.               }
  243.             return dref;
  244.           }
  245.       }
  246.  
  247.     /*
  248.      * Name mentioned in some op_def,
  249.      * but not in initial_dictionaries.
  250.      */
  251.     abort();
  252. }
  253.  
  254. /* Initialize objects other than operators.  In particular, */
  255. /* initialize the dictionaries that hold operator definitions. */
  256. void
  257. obj_init(void)
  258. {    bool local = ialloc_is_local(idmemory);
  259.     bool level2 = gs_have_level2();
  260.  
  261.     /* Initialize the language level. */
  262.     make_int(&ref_language_level, 1);
  263.  
  264.     /* Initialize the interpreter. */
  265.     gs_interp_init();
  266.  
  267.     {
  268. #define icount countof(initial_dictionaries)
  269.       ref idicts[icount];
  270.       int i;
  271.       op_def_ptr _ds *tptr;
  272.  
  273.       min_dstack_size = MIN_DSTACK_SIZE;
  274.  
  275.       refset_null(idicts, icount);
  276.  
  277.       ialloc_set_local(idmemory, false);
  278.       if ( level2 )
  279.         {    dsp += 2;
  280.         dict_create(SYSTEMDICT_LEVEL2_SIZE, dsp);
  281.         /* For the moment, let globaldict be an alias */
  282.         /* for systemdict. */
  283.         dsp[-1] = *dsp;
  284.         min_dstack_size++;
  285.         }
  286.       else
  287.         {    ++dsp;
  288.         dict_create(SYSTEMDICT_SIZE, dsp);
  289.         }
  290.       ref_systemdict = *dsp;
  291.       ialloc_set_local(idmemory, local);
  292.  
  293.       /* Create dictionaries which are to be homes for operators. */
  294.       for ( tptr = op_defs_all; *tptr != 0; tptr++ )
  295.         {    const op_def _ds *def;
  296.         for ( def = *tptr; def->oname != 0; def++ )
  297.           if ( op_def_is_begin_dict(def) )
  298.             make_initial_dict(def->oname, strlen(def->oname), idicts);
  299.         }
  300.  
  301.       /* Set up the initial dstack. */
  302.       for ( i = 0; i < countof(initial_dstack); i++ )
  303.         {    const char *dname = initial_dstack[i];
  304.         ++dsp;
  305.         ref_assign(dsp, 
  306.                make_initial_dict(dname, strlen(dname), idicts));
  307.         }
  308.  
  309.       /* Enter names of referenced initial dictionaries into systemdict. */
  310.       initial_enter_name("systemdict", &ref_systemdict);
  311.       for (i = 0; i < icount; i++)
  312.         {    ref *idict = &idicts[i];
  313.         if ( !r_has_type(idict, t_null) )
  314.           {    /*
  315.              * Note that we enter the dictionary in systemdict
  316.              * even if it is in local VM.  There is a special
  317.              * provision in the garbage collector for this:
  318.              * see ivmspace.h for more information.
  319.              * In order to do this, we must temporarily
  320.              * identify systemdict as local, so that the
  321.              * store check in dict_put won't fail.
  322.              */
  323.             r_set_attrs(systemdict, a_local);
  324.             initial_enter_name(initial_dictionaries[i].name,
  325.                        idict);
  326.             r_clear_attrs(systemdict, a_local);
  327.           }
  328.         }
  329. #undef icount
  330.     }
  331.  
  332.     gs_interp_reset();
  333.  
  334.     {    ref vtemp;
  335.         make_null(&vtemp);
  336.         initial_enter_name("null", &vtemp);
  337.     }
  338.  
  339.     /* Create the error name table */
  340.     {    int n = countof(gs_error_names) - 1;
  341.         int i;
  342.         ref era;
  343.         ialloc_ref_array(&era, a_readonly, n, "ErrorNames");
  344.         for ( i = 0; i < n; i++ )
  345.           name_enter((char *)gs_error_names[i], era.value.refs + i);
  346.         dict_put_string(systemdict, "ErrorNames", &era);
  347.     }
  348. }
  349.  
  350. /* Run the initialization procedures of the individual operator files. */
  351. void
  352. zop_init(void)
  353. {    op_def_ptr _ds *tptr;
  354.     /* Because of a bug in Sun's SC1.0 compiler, */
  355.     /* we have to spell out the typedef for op_def_ptr here: */
  356.     const op_def _ds *def;
  357.     for ( tptr = op_defs_all; *tptr != 0; tptr++ )
  358.       {    for ( def = *tptr; def->oname != 0; def++ )
  359.           DO_NOTHING;
  360.         if ( def->proc != 0 )
  361.           ((void (*)(P0()))(def->proc))();
  362.       }
  363.  
  364.     /* Initialize the predefined names other than operators. */
  365.     /* Do this here in case op_init changed any of them. */
  366.     {    ref vtemp;
  367.         make_const_string(&vtemp, a_readonly | a_foreign,
  368.                   strlen(gs_copyright),
  369.                   (const byte *)gs_copyright);
  370.         initial_enter_name("copyright", &vtemp);
  371.         make_const_string(&vtemp, a_readonly | a_foreign,
  372.                   strlen(gs_product),
  373.                   (const byte *)gs_product);
  374.         initial_enter_name("product", &vtemp);
  375.         make_int(&vtemp, gs_revision);
  376.         initial_enter_name("revision", &vtemp);
  377.         make_int(&vtemp, gs_revisiondate);
  378.         initial_enter_name("revisiondate", &vtemp);
  379.     }
  380. }
  381. /* Initialize the operator table. */
  382. void
  383. op_init(void)
  384. {    int count = 1;
  385.     op_def_ptr _ds *tptr;
  386.     /* Because of a bug in Sun's SC1.0 compiler, */
  387.     /* we have to spell out the typedef for op_def_ptr here: */
  388.     const op_def _ds *def;
  389.     const char _ds *nstr;
  390.  
  391.     /* Do a first pass just to count the operators. */
  392.  
  393.     for ( tptr = op_defs_all; *tptr != 0; tptr ++ )
  394.     {    for ( def = *tptr; def->oname != 0; def++ )
  395.           if ( !op_def_is_begin_dict(def) )
  396.             count++;
  397.     }
  398.     
  399.     /* Do a second pass to construct the operator table, */
  400.     /* and enter the operators into the appropriate dictionary. */
  401.  
  402.     /* Because of a bug in Sun's SC1.0 compiler, */
  403.     /* we have to spell out the typedef for op_def_ptr here: */
  404.     op_def_table =
  405.       (const op_def _ds **)ialloc_byte_array(count, sizeof(op_def_ptr),
  406.                          "op_init(op_def_table)");
  407.     op_def_count = count;
  408.     count = 1;
  409.     for ( tptr = op_defs_all; *tptr != 0; tptr ++ )
  410.       {    ref *pdict = systemdict;
  411.         for ( def = *tptr; (nstr = def->oname) != 0; def++ )
  412.           if ( op_def_is_begin_dict(def) )
  413.             {    int code;
  414.             ref nref;
  415.             code = name_ref((byte *)nstr, strlen(nstr), &nref, -1);
  416.             if ( code != 0 ) abort();
  417.             if ( !dict_find(systemdict, &nref, &pdict)) abort();
  418.             if ( !r_has_type(pdict, t_dictionary) ) abort();
  419.             }
  420.           else
  421.             {    ref nref, oper;
  422.             make_oper(&oper, count, def->proc);
  423.             gs_interp_fix_op(&oper);    /* optimize if possible */
  424.             /* The first character of the name is a digit */
  425.             /* giving the minimum acceptable number of operands. */
  426.             /* For now, we just skip over it. */
  427.             nstr++;
  428.             /* Don't enter internal operators into */
  429.             /* the dictionary. */
  430.             if ( *nstr == '%' )
  431.               name_enter(nstr, &nref);
  432.             else
  433.               dict_put_string(pdict, nstr, &oper);
  434.             op_def_table[count] = def;
  435.             count++;
  436.             }
  437.       }
  438.     gs_register_struct_root(imemory, &op_def_root,
  439.                 (void **)&op_def_table, "op_def_table");
  440.  
  441.     /* Allocate the table for `operator' procedures. */
  442.  
  443.     ialloc_ref_array(&op_array_table, a_readonly, OP_ARRAY_TABLE_SIZE,
  444.              "op_array table");
  445.     refset_null(op_array_table.value.refs, OP_ARRAY_TABLE_SIZE);
  446.     gs_register_ref_root(imemory, &op_array_root, (void **)&op_array_p,
  447.                  "op_array_table");
  448.     op_array_nx_table =
  449.       (ushort *)ialloc_byte_array(OP_ARRAY_TABLE_SIZE, sizeof(ushort),
  450.                       "op_array nx table");
  451.     op_array_count = 0;
  452.     gs_register_struct_root(imemory, &op_array_nx_root,
  453.                 (void **)&op_array_nx_table,
  454.                 "op_array nx table");
  455.  
  456. }
  457.