home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Magazine / UsingPDF / GhostScript / source / gs5.10 / imain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-30  |  17.4 KB  |  613 lines

  1. /* Copyright (C) 1989, 1996, 1997 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. /* imain.c */
  20. /* Common support for interpreter front ends */
  21. #include "memory_.h"
  22. #include "string_.h"
  23. /* Capture stdin/out/err before gs.h redefines them. */
  24. #include <stdio.h>
  25. void
  26. gs_get_real_stdio(FILE *stdfiles[3])
  27. {    stdfiles[0] = stdin;
  28.     stdfiles[1] = stdout;
  29.     stdfiles[2] = stderr;
  30. }
  31. #include "ghost.h"
  32. #include "gp.h"
  33. #include "gslib.h"
  34. #include "gsmatrix.h"            /* for gxdevice.h */
  35. #include "gsutil.h"            /* for bytes_compare */
  36. #include "gxdevice.h"
  37. #include "errors.h"
  38. #include "oper.h"
  39. #include "idebug.h"
  40. #include "idict.h"
  41. #include "iname.h"            /* for name_init */
  42. #include "dstack.h"
  43. #include "estack.h"
  44. #include "ostack.h"            /* put here for files.h */
  45. #include "stream.h"            /* for files.h */
  46. #include "files.h"
  47. #include "ialloc.h"
  48. #include "strimpl.h"        /* for sfilter.h */
  49. #include "sfilter.h"        /* for iscan.h */
  50. #include "iscan.h"
  51. #include "main.h"
  52. #include "store.h"
  53. #include "isave.h"        /* for prototypes */
  54. #include "interp.h"
  55. #include "ivmspace.h"
  56.  
  57. /* ------ Exported data ------ */
  58.  
  59. /* Define the default instance of the interpreter. */
  60. /* Currently, this is the *only possible* instance, because most of */
  61. /* the places that need to take an explicit instance argument don't. */
  62. private gs_main_instance the_gs_main_instance;
  63. gs_main_instance *
  64. gs_main_instance_default(void)
  65. {    /* Determine whether the instance has been initialized. */
  66.     if ( the_gs_main_instance.memory_chunk_size == 0 )
  67.       the_gs_main_instance = gs_main_instance_init_values;
  68.     return &the_gs_main_instance;
  69. }
  70.  
  71. /* The only reason we export gs_exit_status is so that window systems */
  72. /* with alert boxes can know whether to pause before exiting if */
  73. /* the program terminates with an error.  There must be a better way .... */
  74. int gs_exit_status;
  75.  
  76. /* ------ Imported data ------ */
  77.  
  78. /* Configuration information imported from gconfig.c and iinit.c. */
  79. extern const char *gs_init_file;
  80. extern const byte far_data gs_init_string[];
  81. extern const uint far_data gs_init_string_sizeof;
  82. extern ref gs_init_file_array[];
  83. extern ref gs_emulator_name_array[];
  84.  
  85. /* ------ Forward references ------ */
  86.  
  87. private int gs_run_init_file(P3(gs_main_instance *, int *, ref *));
  88. #ifdef DEBUG
  89. private void print_usage(P3(const gs_main_instance *, gs_dual_memory_t *,
  90.                 const char *));
  91. #endif
  92.  
  93. /* ------ Initialization ------ */
  94.  
  95. /* A handy way to declare and execute an initialization procedure: */
  96. #define call_init(proc)\
  97. { extern void proc(P0()); proc(); }
  98.  
  99. /* Initialization to be done before anything else. */
  100. void
  101. gs_main_init0(gs_main_instance *minst, FILE *in, FILE *out, FILE *err,
  102.   int max_lib_paths)
  103. {    /* Set our versions of stdin/out/err. */
  104.     gs_stdin = in;
  105.     gs_stdout = out;
  106.     gs_stderr = err;
  107.     /* Do platform-dependent initialization. */
  108.     /* We have to do this as the very first thing, */
  109.     /* because it detects attempts to run 80N86 executables (N>0) */
  110.     /* on incompatible processors. */
  111.     gp_init();
  112. #ifdef DEBUG
  113.     gp_get_usertime(minst->base_time);
  114. #endif
  115.     /* Initialize the imager. */
  116.     gs_lib_init0(gs_stdout);
  117.     /* Initialize the file search paths. */
  118.     make_array(&minst->lib_path.container, avm_foreign, max_lib_paths,
  119.            (ref *)gs_malloc(max_lib_paths, sizeof(ref),
  120.                     "lib_path array"));
  121.     make_array(&minst->lib_path.list, avm_foreign | a_readonly, 0,
  122.            minst->lib_path.container.value.refs);
  123.     minst->lib_path.env = 0;
  124.     minst->lib_path.final = 0;
  125.     minst->lib_path.count = 0;
  126.     minst->user_errors = 1;
  127.     minst->init_done = 0;
  128. }
  129.  
  130. /* Initialization to be done before constructing any objects. */
  131. void
  132. gs_main_init1(gs_main_instance *minst)
  133. {    if ( minst->init_done < 1 )
  134.     {    {    extern bool gs_have_level2(P0());
  135.             ialloc_init(&gs_memory_default,
  136.                     minst->memory_chunk_size,
  137.                     gs_have_level2());
  138.             gs_lib_init1((gs_memory_t *)imemory_system);
  139.             alloc_save_init(idmemory);
  140.         }
  141.         if ( name_init(minst->name_table_size, imemory_system) == 0 )
  142.         {    puts("name_init failed");
  143.             gs_exit(1);
  144.         }
  145.         call_init(obj_init)        /* requires name_init */
  146.         call_init(scan_init)        /* ditto */
  147.         minst->init_done = 1;
  148.     }
  149. }
  150.  
  151. /* Initialization to be done before running any files. */
  152. private void
  153. init2_make_string_array(ref *srefs, const char *aname)
  154. {    ref *ifp = srefs;
  155.     ref ifa;
  156.     for ( ; ifp->value.bytes != 0; ifp++ )
  157.       r_set_size(ifp, strlen((const char *)ifp->value.bytes));
  158.     make_tasv(&ifa, t_array, a_readonly | avm_foreign,
  159.           ifp - srefs, refs, srefs);
  160.     initial_enter_name(aname, &ifa);
  161. }
  162. void
  163. gs_main_init2(gs_main_instance *minst)
  164. {    gs_main_init1(minst);
  165.     if ( minst->init_done < 2 )
  166.        {    int code, exit_code;
  167.         ref error_object;
  168.         call_init(igs_init)
  169.         call_init(zop_init)
  170.         {    extern void gs_iodev_init(P1(gs_memory_t *));
  171.             gs_iodev_init(imemory);
  172.         }
  173.         call_init(op_init)    /* requires obj_init, scan_init */
  174.  
  175.         /* Set up the array of additional initialization files. */
  176.         init2_make_string_array(gs_init_file_array, "INITFILES");
  177.         /* Set up the array of emulator names. */
  178.         init2_make_string_array(gs_emulator_name_array, "EMULATORS");
  179.         /* Pass the search path. */
  180.         initial_enter_name("LIBPATH", &minst->lib_path.list);
  181.  
  182.         /* Execute the standard initialization file. */
  183.         code = gs_run_init_file(minst, &exit_code, &error_object);
  184.         if ( code < 0 )
  185.         {    if ( code != e_Fatal )
  186.                 gs_debug_dump_stack(code, &error_object);
  187.             gs_exit_with_code((exit_code ? exit_code : 2), code);
  188.         }
  189.         minst->init_done = 2;
  190.        }
  191. #ifdef DEBUG
  192.     if ( gs_debug_c(':') )
  193.       print_usage(minst, &gs_imemory, "Start");
  194. #endif
  195. }
  196.  
  197. /* ------ Search paths ------ */
  198.  
  199. /* Internal routine to add a set of directories to a search list. */
  200. /* Returns 0 or an error code. */
  201. private int
  202. file_path_add(gs_file_path *pfp, const char *dirs)
  203. {    uint len = r_size(&pfp->list);
  204.     const char *dpath = dirs;
  205.  
  206.     if ( dirs == 0 )
  207.       return 0;
  208.     for ( ; ; )
  209.        {    /* Find the end of the next directory name. */
  210.         const char *npath = dpath;
  211.  
  212.         while ( *npath != 0 && *npath != gp_file_name_list_separator )
  213.           npath++;
  214.         if ( npath > dpath )
  215.           {    if ( len == r_size(&pfp->container) )
  216.               return_error(e_limitcheck);
  217.             make_const_string(&pfp->container.value.refs[len],
  218.                       avm_foreign | a_readonly,
  219.                       npath - dpath, (const byte *)dpath);
  220.             ++len;
  221.           }
  222.         if ( !*npath )
  223.           break;
  224.         dpath = npath + 1;
  225.        }
  226.     r_set_size(&pfp->list, len);
  227.     return 0;
  228. }
  229.  
  230. /* Add a library search path to the list. */
  231. void
  232. gs_main_add_lib_path(gs_main_instance *minst, const char *lpath)
  233. {    /* Account for the possibility that the first element */
  234.     /* is gp_current_directory name added by set_lib_paths. */
  235.     int first_is_here =
  236.       (r_size(&minst->lib_path.list) != 0 &&
  237.        minst->lib_path.container.value.refs[0].value.bytes ==
  238.        (const byte *)gp_current_directory_name ? 1 : 0);
  239.  
  240.     r_set_size(&minst->lib_path.list, minst->lib_path.count +
  241.            first_is_here);
  242.     file_path_add(&minst->lib_path, lpath);
  243.     minst->lib_path.count = r_size(&minst->lib_path.list) - first_is_here;
  244.     gs_main_set_lib_paths(minst);
  245. }
  246.  
  247. /* ------ Execution ------ */
  248.  
  249. /* Complete the list of library search paths. */
  250. /* This may involve adding or removing the current directory */
  251. /* as the first element. */
  252. void
  253. gs_main_set_lib_paths(gs_main_instance *minst)
  254. {    ref *paths = minst->lib_path.container.value.refs;
  255.     int first_is_here =
  256.       (r_size(&minst->lib_path.list) != 0 &&
  257.        paths[0].value.bytes == (const byte *)gp_current_directory_name ? 1 : 0);
  258.     int count = minst->lib_path.count;
  259.  
  260.     if ( minst->search_here_first )
  261.       { if ( !(first_is_here ||
  262.            (r_size(&minst->lib_path.list) != 0 &&
  263.             !bytes_compare((const byte *)gp_current_directory_name,
  264.                    strlen(gp_current_directory_name),
  265.                    paths[0].value.bytes,
  266.                    r_size(&paths[0]))))
  267.            )
  268.           { memmove(paths + 1, paths, count * sizeof(*paths));
  269.         make_const_string(paths, avm_foreign | a_readonly,
  270.                   strlen(gp_current_directory_name),
  271.                   (const byte *)gp_current_directory_name);
  272.           }
  273.       }
  274.     else
  275.       { if ( first_is_here )
  276.           memmove(paths, paths + 1, count * sizeof(*paths));
  277.       }
  278.     r_set_size(&minst->lib_path.list,
  279.            count + (minst->search_here_first ? 1 : 0));
  280.     if ( minst->lib_path.env != 0 )
  281.       file_path_add(&minst->lib_path, minst->lib_path.env);
  282.     if ( minst->lib_path.final != 0 )
  283.       file_path_add(&minst->lib_path, minst->lib_path.final);
  284. }
  285.  
  286. /* Open a file, using the search paths. */
  287. int
  288. gs_main_lib_open(gs_main_instance *minst, const char *file_name, ref *pfile)
  289. {    /* This is a separate procedure only to avoid tying up */
  290.     /* extra stack space while running the file. */
  291. #define maxfn 200
  292.     byte fn[maxfn];
  293.     uint len;
  294.     return lib_file_open(file_name, strlen(file_name), fn, maxfn,
  295.                  &len, pfile);
  296. }
  297.  
  298. /* Open and execute a file. */
  299. int
  300. gs_main_run_file(gs_main_instance *minst, const char *file_name, int user_errors, int *pexit_code, ref *perror_object)
  301. {    ref initial_file;
  302.     int code = gs_main_run_file_open(minst, file_name, &initial_file);
  303.     if ( code < 0 ) return code;
  304.     return gs_interpret(&initial_file, user_errors, pexit_code, perror_object);
  305. }
  306. int
  307. gs_main_run_file_open(gs_main_instance *minst, const char *file_name, ref *pfref)
  308. {    gs_main_set_lib_paths(minst);
  309.     if ( gs_main_lib_open(minst, file_name, pfref) < 0 )
  310.     {    eprintf1("Can't find initialization file %s.\n", file_name);
  311.         return_error(e_Fatal);
  312.     }
  313.     r_set_attrs(pfref, a_execute + a_executable);
  314.     return 0;
  315. }
  316.  
  317. /* Open and run the very first initialization file. */
  318. private int
  319. gs_run_init_file(gs_main_instance *minst, int *pexit_code, ref *perror_object)
  320. {    ref ifile;
  321.     ref first_token;
  322.     int code;
  323.     scanner_state state;
  324.  
  325.     gs_main_set_lib_paths(minst);
  326.     if ( gs_init_string_sizeof == 0 )
  327.       {    /* Read from gs_init_file. */
  328.         code = gs_main_run_file_open(minst, gs_init_file, &ifile);
  329.       }
  330.     else
  331.       {    /* Read from gs_init_string. */
  332.         code = file_read_string(gs_init_string, gs_init_string_sizeof,
  333.                     &ifile);
  334.       }
  335.     if ( code < 0 )
  336.       {    *pexit_code = 255;
  337.         return code;
  338.       }
  339.     /* Check to make sure the first token is an integer */
  340.     /* (for the version number check.) */
  341.     scanner_state_init(&state, false);
  342.     code = scan_token(ifile.value.pfile, &first_token, &state);
  343.     if ( code != 0 || !r_has_type(&first_token, t_integer) )
  344.     {    eprintf1("Initialization file %s does not begin with an integer.\n", gs_init_file);
  345.         *pexit_code = 255;
  346.         return_error(e_Fatal);
  347.     }
  348.     *++osp = first_token;
  349.     r_set_attrs(&ifile, a_executable);
  350.     return gs_interpret(&ifile, minst->user_errors,
  351.                 pexit_code, perror_object);
  352. }
  353.  
  354. /* Run a string. */
  355. int
  356. gs_main_run_string(gs_main_instance *minst, const char *str, int user_errors,
  357.   int *pexit_code, ref *perror_object)
  358. {    return gs_main_run_string_with_length(minst, str, (uint)strlen(str),
  359.                           user_errors,
  360.                           pexit_code, perror_object);
  361. }
  362. int
  363. gs_main_run_string_with_length(gs_main_instance *minst, const char *str,
  364.   uint length, int user_errors, int *pexit_code, ref *perror_object)
  365. {    int code;
  366.     code = gs_main_run_string_begin(minst, user_errors,
  367.                     pexit_code, perror_object);
  368.     if ( code < 0 )
  369.       return code;
  370.     code = gs_main_run_string_continue(minst, str, length, user_errors,
  371.                        pexit_code, perror_object);
  372.     if ( code != e_NeedInput )
  373.       return code;
  374.     return gs_main_run_string_end(minst, user_errors,
  375.                       pexit_code, perror_object);
  376. }
  377.  
  378. /* Set up for a suspendable run_string. */
  379. int
  380. gs_main_run_string_begin(gs_main_instance *minst, int user_errors,
  381.   int *pexit_code, ref *perror_object)
  382. {    const char *setup = ".runstringbegin";
  383.     ref rstr;
  384.     int code;
  385.  
  386.     gs_main_set_lib_paths(minst);
  387.     make_const_string(&rstr, avm_foreign | a_readonly | a_executable,
  388.               strlen(setup), (const byte *)setup);
  389.     code = gs_interpret(&rstr, user_errors, pexit_code, perror_object);
  390.     return (code == e_NeedInput ? 0 : code == 0 ? e_Fatal : code);
  391. }
  392. /* Continue running a string with the option of suspending. */
  393. int
  394. gs_main_run_string_continue(gs_main_instance *minst, const char *str,
  395.   uint length, int user_errors, int *pexit_code, ref *perror_object)
  396. {    ref rstr;
  397.  
  398.     if ( length == 0 )
  399.       return 0;        /* empty string signals EOF */
  400.     make_const_string(&rstr, avm_foreign | a_readonly, length,
  401.               (const byte *)str);
  402.     return gs_interpret(&rstr, user_errors, pexit_code, perror_object);
  403. }
  404. /* Signal EOF when suspended. */
  405. int
  406. gs_main_run_string_end(gs_main_instance *minst, int user_errors,
  407.   int *pexit_code, ref *perror_object)
  408. {    ref rstr;
  409.  
  410.     make_empty_const_string(&rstr, avm_foreign | a_readonly);
  411.     return gs_interpret(&rstr, user_errors, pexit_code, perror_object);
  412. }
  413.  
  414. /* ------ Operand stack access ------ */
  415.  
  416. /* These are built for comfort, not for speed. */
  417.  
  418. private int
  419. push_value(ref *pvalue)
  420. {    int code = ref_stack_push(&o_stack, 1);
  421.     if ( code < 0 )
  422.       return code;
  423.     *ref_stack_index(&o_stack, 0L) = *pvalue;
  424.     return 0;
  425. }
  426.  
  427. int
  428. gs_push_boolean(gs_main_instance *minst, bool value)
  429. {    ref vref;
  430.     make_bool(&vref, value);
  431.     return push_value(&vref);
  432. }
  433.  
  434. int
  435. gs_push_integer(gs_main_instance *minst, long value)
  436. {    ref vref;
  437.     make_int(&vref, value);
  438.     return push_value(&vref);
  439. }
  440.  
  441. int
  442. gs_push_real(gs_main_instance *minst, floatp value)
  443. {    ref vref;
  444.     make_real(&vref, value);
  445.     return push_value(&vref);
  446. }
  447.  
  448. int
  449. gs_push_string(gs_main_instance *minst, byte *chars, uint length,
  450.   bool read_only)
  451. {    ref vref;
  452.     make_string(&vref, avm_foreign | (read_only ? a_readonly : a_all),
  453.             length, (byte *)chars);
  454.     return push_value(&vref);
  455. }
  456.  
  457. private int
  458. pop_value(ref *pvalue)
  459. {    if ( !ref_stack_count(&o_stack) )
  460.       return_error(e_stackunderflow);
  461.     *pvalue = *ref_stack_index(&o_stack, 0L);
  462.     return 0;
  463. }
  464.  
  465. int
  466. gs_pop_boolean(gs_main_instance *minst, bool *result)
  467. {    ref vref;
  468.     int code = pop_value(&vref);
  469.     if ( code < 0 )
  470.       return code;
  471.     check_type_only(vref, t_boolean);
  472.     *result = vref.value.boolval;
  473.     ref_stack_pop(&o_stack, 1);
  474.     return 0;
  475. }
  476.  
  477. int
  478. gs_pop_integer(gs_main_instance *minst, long *result)
  479. {    ref vref;
  480.     int code = pop_value(&vref);
  481.     if ( code < 0 )
  482.       return code;
  483.     check_type_only(vref, t_integer);
  484.     *result = vref.value.intval;
  485.     ref_stack_pop(&o_stack, 1);
  486.     return 0;
  487. }
  488.  
  489. int
  490. gs_pop_real(gs_main_instance *minst, float *result)
  491. {    ref vref;
  492.     int code = pop_value(&vref);
  493.     if ( code < 0 )
  494.       return code;
  495.     switch ( r_type(&vref) )
  496.       {
  497.       case t_real:
  498.         *result = vref.value.realval;
  499.         break;
  500.       case t_integer:
  501.         *result = vref.value.intval;
  502.         break;
  503.       default:
  504.         return_error(e_typecheck);
  505.       }
  506.     ref_stack_pop(&o_stack, 1);
  507.     return 0;
  508. }
  509.  
  510. int
  511. gs_pop_string(gs_main_instance *minst, gs_string *result)
  512. {    ref vref;
  513.     int code = pop_value(&vref);
  514.     if ( code < 0 )
  515.       return code;
  516.     switch ( r_type(&vref) )
  517.       {
  518.       case t_name:
  519.         name_string_ref(&vref, &vref);
  520.         code = 1;
  521.         goto rstr;
  522.       case t_string:
  523.         code = (r_has_attr(&vref, a_write) ? 0 : 1);
  524. rstr:        result->data = vref.value.bytes;
  525.         result->size = r_size(&vref);
  526.         break;
  527.       default:
  528.         return_error(e_typecheck);
  529.       }
  530.     ref_stack_pop(&o_stack, 1);
  531.     return code;
  532. }
  533.  
  534. /* ------ Termination ------ */
  535.  
  536. /* Free all resources and exit. */
  537. void
  538. gs_main_finit(gs_main_instance *minst, int exit_status, int code)
  539. {    /*
  540.      * Previous versions of this code closed the devices in the
  541.      * device list here.  Since these devices are now prototypes,
  542.      * they cannot be opened, so they do not need to be closed;
  543.      * alloc_restore_all will close dynamically allocated devices.
  544.      */
  545.     gs_exit_status = exit_status;    /* see above */
  546.  
  547. #ifdef DEBUG
  548.     if ( gs_debug_c(':') )
  549.       print_usage(minst, &gs_imemory, "Final");
  550. #endif
  551.     /* Do the equivalent of a restore "past the bottom". */
  552.     /* This will release all memory, close all open files, etc. */
  553.     if ( minst->init_done >= 1 )
  554.       alloc_restore_all(idmemory);
  555.     gs_lib_finit(exit_status, code);
  556. }
  557. void
  558. gs_exit_with_code(int exit_status, int code)
  559. {    gs_finit(exit_status, code);
  560.     gp_do_exit(exit_status);
  561. }
  562. void
  563. gs_exit(int exit_status)
  564. {    gs_exit_with_code(exit_status, 0);
  565. }
  566.  
  567. /* ------ Debugging ------ */
  568.  
  569. #ifdef DEBUG
  570. /* Print usage statistics. */
  571. private void
  572. print_usage(const gs_main_instance *minst, gs_dual_memory_t *dmem,
  573.   const char *msg)
  574. {    ulong allocated = 0, used = 0;
  575.     long utime[2];
  576.  
  577.     gp_get_usertime(utime);
  578.     { int i;
  579.  
  580.       for ( i = 0; i < countof(dmem->spaces.indexed); ++i ) {
  581.         gs_ref_memory_t *mem = dmem->spaces.indexed[i];
  582.  
  583.         if ( mem != 0 && (i == 0 || mem != dmem->spaces.indexed[i - 1]) ) {
  584.           gs_memory_status_t status;
  585.  
  586.           gs_memory_status((gs_memory_t *)mem, &status);
  587.           allocated += status.allocated;
  588.           used += status.used;
  589.         }
  590.       }
  591.     }
  592.     dprintf4("%% %s time = %g, memory allocated = %lu, used = %lu\n",
  593.          msg, utime[0] - minst->base_time[0] +
  594.            (utime[1] - minst->base_time[1]) / 1000000000.0,
  595.          allocated, used);
  596. }
  597. #endif
  598.  
  599. /* Dump the stacks after interpretation */
  600. void
  601. gs_debug_dump_stack(int code, ref *perror_object)
  602. {    zflush(osp);    /* force out buffered output */
  603.     dprintf1("\nUnexpected interpreter error %d.\n", code);
  604.     if ( perror_object != 0 )
  605.     {    dputs("Error object: ");
  606.         debug_print_ref(perror_object);
  607.         dputc('\n');
  608.     }
  609.     debug_dump_stack(&o_stack, "Operand stack");
  610.     debug_dump_stack(&e_stack, "Execution stack");
  611.     debug_dump_stack(&d_stack, "Dictionary stack");
  612. }
  613.