/* gsdll_execute_end, then gsdll_exit, then unload library */ int GSDLLEXPORT GSDLLAPI gsdll_execute_cont(const char * str, int len) { int exit_code; int code = gsapi_run_string_continue(pgs_minst, str, len, 0, &exit_code); if (code == e_NeedInput) code = 0; /* this is not an error */ return code; }
/* Wrap up interp instance after a "job" */ static int /* ret 0 ok, else -ve error code */ ps_impl_dnit_job( pl_interp_instance_t *instance /* interp instance to wrap up job in */ ) { int code = 0; int exit_code = 0; static const char *buf = "\n.endjob\n"; /* restore to initial state, non-encapsualted */ ps_interp_instance_t *psi = (ps_interp_instance_t *)instance; /* take care of a stored pdf file */ if ( psi->pdf_stream ) { /* * Hex encode to avoid problems with window's directory * separators '\' being interpreted in postscript as escape * sequences. The buffer length is the maximum file name size * (gp_file_name_sizeof) * 2 (hex encoding) + 2 (hex * delimiters '<' and '>') + the rest of the run command 7 * (space + (run) + new line + null). */ char buf[gp_file_name_sizeof * 2 + 2 + 7]; const char *run_str = " run\n"; const char *hex_digits = "0123456789ABCDEF"; char *pd = buf; /* destination */ const char *ps = psi->pdf_file_name; /* source */ *pd = '<'; pd++; while (*ps) { *pd = hex_digits[*ps >> 4 ]; pd++; *pd = hex_digits[*ps & 0xf]; pd++; ps++; } *pd = '>'; pd++; strcpy(pd, run_str); /* at this point we have finished writing the spooled pdf file and we need to close it */ fclose(psi->pdf_filep); /* Send the buffer to Ghostscript */ code = gsapi_run_string_continue(psi->plmemory->gs_lib_ctx, buf, strlen(buf), 0, &exit_code); /* indicate we are done with the pdf stream */ psi->pdf_stream = false; unlink(psi->pdf_file_name); /* handle errors... normally job deinit failures are considered fatal but pdf runs the spooled job when the job is deinitialized so handle error processing here and return code is always 0. */ if (( code < 0) && (code != e_NeedInput)) { errprintf(psi->plmemory, "PDF interpreter exited with exit code %d\n", exit_code); errprintf(psi->plmemory, "Flushing to EOJ\n"); } code = 0; }
/* Prepare interp instance for the next "job" */ static int /* ret 0 ok, else -ve error code */ ps_impl_init_job( pl_interp_instance_t *instance /* interp instance to start job in */ ) { ps_interp_instance_t *psi = (ps_interp_instance_t *)instance; static const char *buf = "\004"; /* use ^D to start a new encapsulated job */ int exit_code; /* starting a new job */ psi->fresh_job = true; gsapi_run_string_continue(psi->plmemory->gs_lib_ctx, buf, strlen(buf), 0, &exit_code); /* ^D */ return 0; }
/* Set a device into an interpreter instance */ static int /* ret 0 ok, else -ve error code */ ps_impl_set_device( pl_interp_instance_t *instance, /* interp instance to use */ gx_device *device /* device to set (open or closed) */ ) { int code = 0; int exit_code = 0; ps_interp_instance_t *psi = (ps_interp_instance_t *)instance; gs_state *pgs = psi->minst->i_ctx_p->pgs; /* Initialize device ICC profile */ code = gsicc_init_device_profile(pgs, device); if (code < 0) return code; /* Set the device into the gstate */ code = gs_setdevice_no_erase(pgs, device); if (code >= 0 ) code = gs_erasepage(pgs); if (code < 0) return code; /* install a screen appropriate for the device */ { const char *screen_str = ".setdefaultscreen\n"; code = gsapi_run_string_continue(psi->plmemory->gs_lib_ctx, screen_str, strlen(screen_str), 0, &exit_code); /* needs more input this is not an error */ if ( code == e_NeedInput ) code = 0; if (code < 0) return code; } return exit_code; }
/* Parse a buffer full of data */ static int /* ret 0 or +ve if ok, else -ve error code */ ps_impl_process( pl_interp_instance_t *instance, /* interp instance to process data job in */ stream_cursor_read *cursor /* data to process */ ) { ps_interp_instance_t *psi = (ps_interp_instance_t *)instance; int code, exit_code; uint avail = cursor->limit - cursor->ptr; /* if we are at the beginning of a job check for pdf and set appropriate state variables to process either a pdf or ps job */ if ( psi->fresh_job ) { const char pdf_idstr[] = "%PDF-1."; /* do we have enough data? */ const uint pdf_idstr_len = strlen(pdf_idstr); if ( avail < pdf_idstr_len ) /* more data. NB update ptr ?? */ return 0; else /* compare beginning of stream with pdf id */ if ( !strncmp(pdf_idstr, (const char *)cursor->ptr + 1, pdf_idstr_len) ) { char fmode[4]; /* open the temporary pdf file. If the file open fails PDF fails and we allow the job to be sent to postscript and generate an error. It turns out this is easier than restoring the state and returning */ strcpy(fmode, "w+"); strcat(fmode, gp_fmode_binary_suffix); psi->pdf_filep = gp_open_scratch_file(psi->plmemory, gp_scratch_file_name_prefix, psi->pdf_file_name, fmode); if ( psi->pdf_filep == NULL ) psi->pdf_stream = false; else psi->pdf_stream = true; } else psi->pdf_stream = false; /* we only check for pdf at the beginning of the job */ psi->fresh_job = false; } /* for a pdf stream we append to the open pdf file but for postscript we hand it directly to the ps interpreter. PDF files are processed subsequently, at end job time */ code = 0; if ( psi->pdf_stream ) { uint bytes_written = fwrite((cursor->ptr + 1), 1, avail, psi->pdf_filep); if ( bytes_written != avail ) code = gs_error_invalidfileaccess; } else { /* Send the buffer to Ghostscript */ code = gsapi_run_string_continue(psi->plmemory->gs_lib_ctx, (const char *)(cursor->ptr + 1), avail, 0, &exit_code); /* needs more input this is not an error */ if ( code == e_NeedInput ) code = 0; /* error - I guess it gets "exit code" - nonsense */ if ( code < 0 ) code = exit_code; } /* update the cursor */ cursor->ptr += avail; /* flush stdout on error. */ if (code < 0) zflush(psi->minst->i_ctx_p); /* return the exit code */ return code; }