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
|
/* Copyright (C) 2001-2012 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 the license contained in the file LICENSE in this distribution.
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.
*/
/* Server front end for Ghostscript, replacing gs.c. */
#include "memory_.h"
#include "string_.h"
#include "ghost.h"
#include "imemory.h" /* for iutil.h */
#include "interp.h" /* for gs_interp_reset */
#include "iutil.h" /* for obj_cvs */
#include "main.h"
#include "ostack.h"
#include "store.h"
#include "gspaint.h" /* for gs_erasepage */
/*
* This file provides a very simple procedural interface to the Ghostscript
* PostScript/PDF language interpreter, with a rudimentary provision for
* saving and restoring state around "jobs".
* See below for the descriptions of individual procedures.
*
* All routines in this file return an integer value which is 0 if the
* routine completed successfully, non-0 if an error occurred.
*/
/* ------ Public interface ------ */
/*
* Initialize Ghostscript. fno_stdin, fno_stdout, and fno_stderr are
* file handles that Ghostscript will use in place of stdin, stdout,
* and stderr respectively. This routine should be called once at the
* beginning of execution, and not after that.
*
* This routine establishes a "baseline" initial state for Ghostscript,
* which includes reading in the standard Ghostscript initialization files
* such as gs_init.ps and gs_fonts.ps. This baseline is always used as the
* starting state for gs_server_run_string and gs_server_run_files, unless
* modified as described below.
*
* This routine 'opens' the default driver.
*/
int gs_server_initialize(int fno_stdin, int fno_stdout, int fno_stderr,
const char *init_str);
/*
* Execute a string containing PostScript code. The effects of this code
* do modify the baseline state for future calls of ...run_string and
* ...run_files. There are four cases of return values:
* value = 0: normal return.
* value = e_Quit: the PostScript code executed a `quit'.
* value = e_Fatal: the PostScript code encountered a fatal error.
* *exit_code_ptr holds the C exit code.
* other value: the PostScript code encountered a PostScript error
* while processing another error, or some other fatal
* PostScript error.
*
* errstr points to a string area of length errstr_max_len for reporting
* the PostScript object causing the error. In the case of an error return,
* the characters from errstr[0] through errstr[*errstr_len_ptr-1] will
* contain a representation of the error object. *errstr_len_ptr will not
* exceed errstr_max_len.
*/
int gs_server_run_string(const char *str, int *exit_code_ptr,
char *errstr, int errstr_max_len,
int *errstr_len_ptr);
/*
* Run a sequence of files containing PostScript code. If permanent is 0,
* the files do not affect the baseline state; if permanent is 1, they do
* affect the baseline state, just like ...run_string. The returned value,
* exit code, and error string are the same as for gs_server_run_string.
*
* If permanent is 0, the output page buffer is cleared before running the
* first file (equivalent to `erasepage').
*/
int gs_server_run_files(const char **file_names, int permanent,
int *exit_code_ptr, char *errstr,
int errstr_max_len, int *errstr_len_ptr);
/*
* Terminate Ghostscript. Ghostscript will release all memory and close
* all files it has opened, including the ones referenced by fno_stdin,
* fno_stdout, and fno_stderr.
*
* This routine 'closes' the default driver.
*/
int gs_server_terminate();
/* ------ Example of use ------ */
/* To run this example, change the 0 to 1 in the following line. */
#if 0
/*
* This example predefines the name fubar, prints out its value,
* and then renders the golfer art file supplied with Ghostscript.
*/
#include <fcntl.h>
#include <sys/stat.h>
int
main(int argc, char *argv[])
{
int code, exit_code;
#define emax 50
char errstr[emax + 1];
int errlen;
static const char *fnames[] =
{"golfer.eps", 0};
FILE *cin = fopen("stdin.tmp", "w+");
int sout = open("stdout.tmp", O_WRONLY | O_CREAT | O_TRUNC,
S_IREAD | S_IWRITE);
int serr = open("stderr.tmp", O_WRONLY | O_CREAT | O_TRUNC,
S_IREAD | S_IWRITE);
code = gs_server_initialize(fileno(cin), sout, serr,
"/fubar 42 def");
fprintf(stdout, "init: code %d\n", code);
if (code < 0)
goto x;
code = gs_server_run_string("fubar == flush", &exit_code,
errstr, emax, &errlen);
fprintf(stdout, "print: code %d\n", code);
if (code < 0)
goto x;
code = gs_server_run_files(fnames, 0, &exit_code,
errstr, emax, &errlen);
fprintf(stdout, "golfer: code %d\n", code);
if (code < 0)
goto x;
errlen = 0;
code = gs_server_run_string("fubar 0 div", &exit_code,
errstr, emax, &errlen);
errstr[errlen] = 0;
fprintf(stdout, "0 div: code %d object %s\n", code, errstr);
errlen = 0;
code = gs_server_run_string("xxx", &exit_code,
errstr, emax, &errlen);
errstr[errlen] = 0;
fprintf(stdout, "undef: code %d object %s\n", code, errstr);
x:code = gs_server_terminate();
fprintf(stdout, "end: code %d\n", code);
fflush(stdout);
close(serr);
close(sout);
fclose(cin);
return code;
}
#endif
/* ------ Private definitions ------ */
/* Forward references */
static int job_begin(void);
static int job_end(void);
static void errstr_report(ref *, char *, int, int *);
/* ------ Public routines ------ */
/* Initialize Ghostscript. */
int
gs_server_initialize(int fno_stdin, int fno_stdout, int fno_stderr,
const char *init_str)
{
int code, exit_code; /* discard exit_code for now */
int errstr_len; /* discard */
FILE *c_stdin, *c_stdout, *c_stderr;
/* Establish C-compatible files for stdout and stderr. */
c_stdin = fdopen(fno_stdin, "r");
if (c_stdin == NULL)
return -1;
c_stdout = fdopen(fno_stdout, "w");
if (c_stdout == NULL)
return -1;
c_stderr = fdopen(fno_stderr, "w");
if (c_stderr == NULL)
return -1;
/* Initialize the Ghostscript interpreter. */
if ((code = gs_init0(c_stdin, c_stdout, c_stderr, 0)) < 0 ||
(code = gs_init1()) < 0 ||
(code = gs_init2()) < 0
)
return code;
code = gs_server_run_string("/QUIET true def /NOPAUSE true def",
&exit_code,
(char *)0, 0, &errstr_len);
if (code < 0)
return code;
return (init_str == NULL ? 0 :
gs_server_run_string(init_str, &exit_code,
(char *)0, 0, &errstr_len));
}
/* Run a string. */
int
gs_server_run_string(const char *str, int *exit_code_ptr,
char *errstr, int errstr_max_len, int *errstr_len_ptr)
{
ref error_object;
int code;
make_tasv(&error_object, t_string, 0, 0, bytes, 0);
code = gs_run_string(str, 0, exit_code_ptr, &error_object);
if (code < 0)
errstr_report(&error_object, errstr, errstr_max_len,
errstr_len_ptr);
return code;
}
/* Run files. */
int
gs_server_run_files(const char **file_names, int permanent,
int *exit_code_ptr, char *errstr, int errstr_max_len, int *errstr_len_ptr)
{
int code = 0;
ref error_object;
const char **pfn;
if (!permanent)
job_begin();
make_tasv(&error_object, t_string, 0, 0, bytes, 0);
for (pfn = file_names; *pfn != NULL && code == 0; pfn++)
code = gs_run_file(*pfn, 0, exit_code_ptr, &error_object);
if (!permanent)
job_end();
if (code < 0)
errstr_report(&error_object, errstr, errstr_max_len,
errstr_len_ptr);
return code;
}
/* Terminate Ghostscript. */
int
gs_server_terminate()
{
gs_finit(0, 0);
return 0;
}
/* ------ Private routines ------ */
static ref job_save; /* 'save' object for baseline state */
extern int zsave(os_ptr), zrestore(os_ptr);
/* Start a 'job' by restoring the baseline state. */
static int
job_begin()
{
int code;
/* Ghostscript doesn't provide erasepage as an operator. */
/* However, we can get the same effect by calling gs_erasepage. */
extern gs_state *igs;
if ((code = gs_erasepage(igs)) < 0)
return code;
code = zsave(osp);
if (code == 0)
job_save = *osp--;
return code;
}
/* End a 'job'. */
static int
job_end()
{
gs_interp_reset();
*++osp = job_save;
return zrestore(osp);
}
/* Produce a printable representation of an error object. */
static void
errstr_report(ref * perror_object, char *errstr, int errstr_max_len,
int *errstr_len_ptr)
{
int code = obj_cvs(perror_object, (byte *) errstr,
(uint) errstr_max_len, (uint *) errstr_len_ptr,
false);
if (code < 0) {
const char *ustr = "[unprintable]";
int len = min(strlen(ustr), errstr_max_len);
memcpy(errstr, ustr, len);
*errstr_len_ptr = len;
}
}
|