home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gs252src.zip / GS252 / GSMAIN.C < prev    next >
C/C++ Source or Header  |  1992-09-12  |  7KB  |  240 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gsmain.c */
  21. /* Common support for Ghostscript front ends */
  22. #include "memory_.h"
  23. #include "string_.h"
  24. #include "ghost.h"
  25. #include "gp.h"
  26. #include "gsmatrix.h"            /* for gxdevice.h */
  27. #include "gxdevice.h"
  28. #include "gserrors.h"
  29. #include "estack.h"
  30. #include "main.h"
  31. #include "ostack.h"
  32. #include "store.h"
  33.  
  34. /* ------ Exported data ------ */
  35.  
  36. uint gs_memory_chunk_size = 20000;
  37. int gs_user_errors;
  38.  
  39. /* File name search paths */
  40. const char **gs_lib_paths;
  41. private int gs_lib_count;
  42. char *gs_lib_env_path;
  43.  
  44. /* Imported data */
  45. /* Configuration information imported from gconfig.c. */
  46. extern const char *gs_lib_default_path;
  47. extern const char *gs_init_file;
  48. extern ref gs_init_file_array[];
  49. /* Imported from gsmisc.c */
  50. extern int gs_log_errors;
  51. extern FILE *gs_debug_out;
  52.  
  53. /* ------ Initialization ------ */
  54.  
  55. /* Remember how much initialization has been done. */
  56. private int init1_done, init2_done;
  57.  
  58. /* A handy way to declare and execute an initialization procedure: */
  59. #define call_init(proc)\
  60. { extern void proc(P0()); proc(); }
  61.  
  62. /* Initialization to be done before anything else. */
  63. void
  64. gs_init0(FILE *in, FILE *out, FILE *err, int max_lib_paths)
  65. {    /* Set the Ghostscript versions of stdin/out/err. */
  66.     gs_stdin = in;
  67.     gs_stdout = out;
  68.     gs_stderr = err;
  69.     gs_debug_out = gs_stdout;
  70.     /* Do platform-dependent initialization. */
  71.     /* We have to do this as the very first thing, */
  72.     /* because it detects attempts to run 80N86 executables (N>0) */
  73.     /* on incompatible processors. */
  74.     gp_init();
  75.     /* Initialize the file search paths */
  76.     gs_lib_env_path = 0;
  77.     gs_lib_paths =
  78.         (const char **)gs_malloc(max_lib_paths + 3, sizeof(char *),
  79.                      "gs_lib_paths array");
  80.     gs_lib_count = 0;
  81.     gs_user_errors = 1;
  82.     gs_log_errors = 0;
  83.     /* Reset debugging flags */
  84.     memset(gs_debug, 0, 128);
  85.     init1_done = init2_done = 0;
  86. }
  87.  
  88. /* Initialization to be done before constructing any objects. */
  89. void
  90. gs_init1()
  91. {    if ( !init1_done )
  92.        {    alloc_init(gs_malloc, gs_free, gs_memory_chunk_size);
  93.         call_init(name_init)
  94.         call_init(obj_init)        /* requires name_init */
  95.         call_init(scan_init)        /* ditto */
  96.         init1_done = 1;
  97.        }
  98. }
  99.  
  100. /* Initialization to be done before running any files. */
  101. void
  102. gs_init2()
  103. {    gs_init1();
  104.     if ( !init2_done )
  105.        {    int code, exit_code;
  106.         ref error_object;
  107.         call_init(gs_init)
  108.         call_init(zop_init)
  109.         interp_init(1);        /* requires obj_init */
  110.         call_init(op_init)    /* requires obj_init, scan_init */
  111.         /* Set up the array of additional initialization files. */
  112.         {    ref *ifp = gs_init_file_array;
  113.             ref ifa;
  114.             for ( ; ifp->value.bytes != 0; ifp++ )
  115.               r_set_size(ifp, strlen((const char *)ifp->value.bytes));
  116.             make_tasv(&ifa, t_array, a_readonly,
  117.                   ifp - gs_init_file_array, refs,
  118.                   gs_init_file_array);
  119.             initial_enter_name("INITFILES", &ifa);
  120.         }
  121.         /* Execute the standard initialization file. */
  122.         code = gs_run_file(gs_init_file, gs_user_errors, &exit_code, &error_object);
  123.         if ( code < 0 )
  124.         {    if ( code != gs_error_Fatal )
  125.                 gs_debug_dump_stack(code, &error_object);
  126.             gs_exit((exit_code ? exit_code : 2));
  127.         }
  128.         init2_done = 1;
  129.        }
  130.    }
  131.  
  132. /* Add a library search path to the list. */
  133. void
  134. gs_add_lib_path(const char *lpath)
  135. {    gs_lib_paths[gs_lib_count] = lpath;
  136.     gs_lib_count++;
  137. }
  138.  
  139. /* ------ Execution ------ */
  140.  
  141. /* Complete the list of library search paths. */
  142. private void
  143. set_lib_paths()
  144. {    const char **ppath = &gs_lib_paths[gs_lib_count];
  145.     if ( gs_lib_env_path != 0 ) *ppath++ = gs_lib_env_path;
  146.     if ( gs_lib_default_path != 0 ) *ppath++ = gs_lib_default_path;
  147.     *ppath = 0;
  148. }
  149.  
  150. /* Open and execute a file */
  151. private int
  152. run_open(const char *file_name, ref *pfile)
  153. {    /* This is a separate procedure only to avoid tying up */
  154.     /* extra stack space while running the file. */
  155. #define maxfn 200
  156.     byte fn[maxfn];
  157.     uint len;
  158.     return lib_file_open(file_name, strlen(file_name), fn, maxfn,
  159.                  &len, pfile);
  160. }
  161. int
  162. gs_run_file(const char *file_name, int user_errors, int *pexit_code, ref *perror_object)
  163. {    ref initial_file;
  164.     set_lib_paths();
  165.     if ( run_open(file_name, &initial_file) < 0 )
  166.        {    eprintf1("Can't find initialization file %s\n", file_name);
  167.         return_error(gs_error_Fatal);
  168.        }
  169.     r_set_attrs(&initial_file, a_execute + a_executable);
  170.     return gs_interpret(&initial_file, user_errors, pexit_code, perror_object);
  171. }
  172.  
  173. int
  174. gs_run_string(const char *str, int user_errors, int *pexit_code, ref *perror_object)
  175. {    ref stref;
  176.     set_lib_paths();
  177.     make_tasv(&stref, t_string, a_executable + a_readonly,
  178.           strlen(str), const_bytes, (byte *)str);
  179.     return gs_interpret(&stref, user_errors, pexit_code, perror_object);
  180. }
  181.  
  182. /* ------ Termination ------ */
  183.  
  184. /* Free all resources and exit. */
  185. extern gx_device *gx_device_list[];
  186. void gs_malloc_release(P0());
  187. void file_close_all(P0());
  188. int gs_closedevice(P1(gx_device *));
  189. void gp_exit(P0());
  190. void gs_finit()
  191. {    gx_device **pdev = gx_device_list;
  192.     fflush(stderr);            /* in case of error exit */
  193.     for ( ; *pdev != 0; pdev++ )
  194.       gs_closedevice(*pdev);
  195.     /* Close all open files. */
  196.     file_close_all();
  197.     /* Release all memory acquired with malloc. */
  198.     gs_malloc_release();
  199.     /* Do platform-specific cleanup. */
  200.     gp_exit();
  201. }
  202. void
  203. gs_exit(int code)
  204. {    gs_finit();
  205.     exit(code);
  206. }
  207.  
  208. /* ------ Debugging ------ */
  209.  
  210. /* Debugging code */
  211. extern void debug_print_ref(P1(const ref *));
  212. extern void debug_dump_refs(P3(const ref *, uint, const char *));
  213.  
  214. /* Dump the stacks after interpretation */
  215. void
  216. gs_debug_dump_stack(int code, ref *perror_object)
  217. {    zflush(osp);    /* force out buffered output */
  218.     dprintf1("\nUnexpected interpreter error %d!\n", code);
  219.     if ( perror_object != 0 )
  220.     {    dputs("Error object: ");
  221.         debug_print_ref(perror_object);
  222.         dputc('\n');
  223.     }
  224.     debug_dump_refs(osbot, osp + 1 - osbot, "Operand stack");
  225.     debug_dump_refs(esbot, esp + 1 - esbot, "Execution stack");
  226. }
  227.  
  228. /* Log an error return */
  229. int
  230. gs_log_error(int err, const char _ds *file, int line)
  231. {    if ( gs_log_errors )
  232.       { if ( file == NULL )
  233.           dprintf1("Returning error %d\n", err);
  234.         else
  235.           dprintf3("%s(%d): returning error %d\n",
  236.                (char *)file, line, err);
  237.       }
  238.     return err;
  239. }
  240.