imain.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* Copyright (C) 1995, 1996, 1999 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: imain.h,v 1.9 2004/08/04 23:33:29 stefan Exp $ */
  14. /* Interface to imain.c */
  15. /* Requires <stdio.h>, stdpre.h, gsmemory.h, gstypes.h, iref.h */
  16. #ifndef imain_INCLUDED
  17. # define imain_INCLUDED
  18. #include "gsexit.h" /* exported by imain.c */
  19. /*
  20. * This file defines the intended API between client front ends
  21. * (such as imainarg.c, the command-line-driven front end)
  22. * and imain.c, which provides top-level control of the interpreter.
  23. */
  24. /* ================ Types ================ */
  25. /*
  26. * Currently, the interpreter has a lot of static variables, but
  27. * eventually it will have none, so that clients will be able to make
  28. * multiple instances of it. In anticipation of this, many of the
  29. * top-level API calls take an interpreter instance (gs_main_instance *)
  30. * as their first argument.
  31. */
  32. #ifndef gs_main_instance_DEFINED
  33. # define gs_main_instance_DEFINED
  34. typedef struct gs_main_instance_s gs_main_instance;
  35. #endif
  36. /* ================ Exported procedures from imain.c ================ */
  37. /* get minst from memory is a hack to allow parts of the system
  38. * to reach minst, slightly better than a global
  39. */
  40. gs_main_instance* get_minst_from_memory(const gs_memory_t *mem);
  41. /* ---------------- Instance creation ---------------- */
  42. /*
  43. * NB: multiple instances are not supported yet
  44. *
  45. * // add usage documentation
  46. */
  47. gs_main_instance *gs_main_alloc_instance(gs_memory_t *);
  48. /* ---------------- Initialization ---------------- */
  49. /*
  50. * The interpreter requires three initialization steps, called init0,
  51. * init1, and init2. These steps must be done in that order, but
  52. * init1 may be omitted.
  53. */
  54. /*
  55. * init0 records the files to be used for stdio, and initializes the
  56. * graphics library, the file search paths, and other instance data.
  57. */
  58. int gs_main_init0(gs_main_instance *minst, FILE *in, FILE *out, FILE *err,
  59. int max_lib_paths);
  60. /*
  61. * init1 initializes the memory manager and other internal data
  62. * structures such as the name table, the token scanner tables,
  63. * dictionaries such as systemdict, and the interpreter stacks.
  64. */
  65. int gs_main_init1(gs_main_instance * minst);
  66. /*
  67. * init2 finishes preparing the interpreter for use by running
  68. * initialization files with PostScript procedure definitions.
  69. */
  70. int gs_main_init2(gs_main_instance * minst);
  71. /*
  72. * The runlibfile operator uses a search path, as described in
  73. * Use.htm, for looking up file names. Each interpreter instance has
  74. * its own search path. The following call adds a directory or set of
  75. * directories to the search path; it is equivalent to the -I command
  76. * line switch. It may be called any time after init0.
  77. */
  78. int gs_main_add_lib_path(gs_main_instance * minst, const char *path);
  79. /*
  80. * Under certain internal conditions, the search path may temporarily
  81. * be in an inconsistent state; gs_main_set_lib_paths takes care of
  82. * this. Clients should never need to call this procedure, and
  83. * eventually it may be removed.
  84. */
  85. int gs_main_set_lib_paths(gs_main_instance * minst);
  86. /*
  87. * Open a PostScript file using the search path. Clients should
  88. * never need to call this procedure, since gs_main_run_file opens the
  89. * file itself, and eventually the procedure may be removed.
  90. */
  91. int gs_main_lib_open(gs_main_instance * minst, const char *fname,
  92. ref * pfile);
  93. /*
  94. * Here we summarize the C API calls that correspond to some of the
  95. * most common command line switches documented in Use.htm, to help
  96. * clients who are familiar with the command line and are starting to
  97. * use the API.
  98. *
  99. * -d/D, -s/S (for setting device parameters like OutputFile)
  100. * Use the C API for device parameters documented near the
  101. * end of gsparam.h.
  102. *
  103. * -d/D (for setting Boolean parameters like NOPAUSE)
  104. * { ref vtrue;
  105. * make_true(&vtrue);
  106. * dict_put_string(systemdict, "NOPAUSE", &vtrue);
  107. * }
  108. * -I
  109. * Use gs_main_add_lib_path, documented above.
  110. *
  111. * -A, -A-
  112. * Set gs_debug['@'] = 1 or 0 respectively.
  113. * -E, -E-
  114. * Set gs_debug['#'] = 1 or 0 respectively.
  115. * -Z..., -Z-...
  116. * Set gs_debug[c] = 1 or 0 respectively for each character
  117. * c in the string.
  118. */
  119. /* ---------------- Execution ---------------- */
  120. /*
  121. * After initializing the interpreter, clients may pass it files or
  122. * strings to be interpreted. There are four ways to do this:
  123. * - Pass a file name (gs_main_run_file);
  124. * - Pass a C string (gs_main_run_string);
  125. * - Pass a string defined by pointer and length
  126. * (gs_main_run_string_with_length);
  127. * - Pass strings piece-by-piece
  128. * (gs_main_run_string_begin/continue/end).
  129. *
  130. * The value returned by the first three of these calls is
  131. * 0 if the interpreter ran to completion, e_Quit for a normal quit,
  132. * or e_Fatal for a non-zero quit or a fatal error.
  133. * e_Fatal stores the exit code in the third argument.
  134. * The str argument of gs_main_run_string[_with_length] must be allocated
  135. * in non-garbage-collectable space (e.g., by malloc or gs_malloc,
  136. * or statically).
  137. */
  138. int gs_main_run_file(gs_main_instance * minst, const char *fname,
  139. int user_errors, int *pexit_code,
  140. ref * perror_object);
  141. int gs_main_run_string(gs_main_instance * minst, const char *str,
  142. int user_errors, int *pexit_code,
  143. ref * perror_object);
  144. int gs_main_run_string_with_length(gs_main_instance * minst,
  145. const char *str, uint length,
  146. int user_errors, int *pexit_code,
  147. ref * perror_object);
  148. /*
  149. * Open the file for gs_main_run_file. This is an internal routine
  150. * that is only exported for some special clients.
  151. */
  152. int gs_main_run_file_open(gs_main_instance * minst,
  153. const char *file_name, ref * pfref);
  154. /*
  155. * The next 3 procedures provide for feeding input to the interpreter
  156. * in arbitrary chunks, unlike run_string, which requires that each string
  157. * be a properly formed PostScript program fragment. To use them:
  158. * Call run_string_begin.
  159. * Call run_string_continue as many times as desired,
  160. * stopping if it returns anything other than e_NeedInput.
  161. * If run_string_continue didn't indicate an error or a quit
  162. * (i.e., a return value other than e_NeedInput), call run_string_end
  163. * to provide an EOF indication.
  164. * Note that run_string_continue takes a pointer and a length, like
  165. * run_string_with_length.
  166. */
  167. int gs_main_run_string_begin(gs_main_instance * minst, int user_errors,
  168. int *pexit_code, ref * perror_object);
  169. int gs_main_run_string_continue(gs_main_instance * minst,
  170. const char *str, uint length,
  171. int user_errors, int *pexit_code,
  172. ref * perror_object);
  173. int gs_main_run_string_end(gs_main_instance * minst, int user_errors,
  174. int *pexit_code, ref * perror_object);
  175. /* ---------------- Operand stack access ---------------- */
  176. /*
  177. * The following procedures are not used in normal operation;
  178. * they exist only to allow clients driving the interpreter through the
  179. * gs_main_run_xxx procedures to push parameters quickly and to get results
  180. * back. The push procedures return 0, e_stackoverflow, or e_VMerror;
  181. * the pop procedures return 0, e_stackunderflow, or e_typecheck.
  182. *
  183. * Procedures to push values on the operand stack:
  184. */
  185. int gs_push_boolean(gs_main_instance * minst, bool value);
  186. int gs_push_integer(gs_main_instance * minst, long value);
  187. int gs_push_real(gs_main_instance * minst, floatp value);
  188. int gs_push_string(gs_main_instance * minst, byte * chars, uint length,
  189. bool read_only);
  190. /*
  191. * Procedures to pop values from the operand stack:
  192. */
  193. int gs_pop_boolean(gs_main_instance * minst, bool * result);
  194. int gs_pop_integer(gs_main_instance * minst, long *result);
  195. int gs_pop_real(gs_main_instance * minst, float *result);
  196. /* gs_pop_string returns 1 if the string is read-only. */
  197. int gs_pop_string(gs_main_instance * minst, gs_string * result);
  198. /* ---------------- Debugging ---------------- */
  199. /*
  200. * Print an error mesage including the error code, error object (if any),
  201. * and operand and execution stacks in hex. Clients will probably
  202. * never call this.
  203. */
  204. void gs_main_dump_stack(gs_main_instance *minst, int code,
  205. ref * perror_object);
  206. /* ---------------- Console output ---------------- */
  207. /* ---------------- Termination ---------------- */
  208. /*
  209. * Terminate the interpreter by closing all devices and releasing all
  210. * allocated memory. Currently, because of some technical problems
  211. * with statically initialized data, it is not possible to reinitialize
  212. * the interpreter after terminating it; we plan to fix this as soon as
  213. * possible.
  214. *
  215. * Note that calling gs_to_exit (defined in gsexit.h) automatically calls
  216. * gs_main_finit for the default instance.
  217. */
  218. int gs_main_finit(gs_main_instance * minst, int exit_status, int code);
  219. #endif /* imain_INCLUDED */