1
/* Copyright (C) 2001-2006 Artifex Software, Inc.
4
This software is provided AS-IS with no warranty, either express or
7
This software is distributed under license and may not be copied, modified
8
or distributed except as expressly authorized under the terms of that
9
license. Refer to licensing information at http://www.artifex.com/
10
or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
11
San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.
14
/*$Id: gslibctx.c 8764 2008-05-21 18:27:42Z mvrhel $ */
16
/* library context functionality for ghostscript
17
* api callers get a gs_main_instance
20
/* Capture stdin/out/err before gs.h redefines them. */
24
gs_lib_ctx_get_real_stdio(FILE **in, FILE **out, FILE **err)
35
static gs_memory_t *mem_err_print = NULL;
39
gs_lib_ctx_get_non_gc_memory_t()
41
return mem_err_print ? mem_err_print->non_gc_memory : NULL;
45
int gs_lib_ctx_init( gs_memory_t *mem )
47
gs_lib_ctx_t *pio = 0;
50
return -1; /* assert mem != 0 */
54
if (mem->gs_lib_ctx) /* one time initialization */
57
pio = mem->gs_lib_ctx =
58
(gs_lib_ctx_t*)gs_alloc_bytes_immovable(mem,
65
gs_lib_ctx_get_real_stdio(&pio->fstdin, &pio->fstdout, &pio->fstderr );
68
pio->stdout_is_redirected = false;
69
pio->stdout_to_stderr = false;
70
pio->stdin_is_interactive = true;
75
pio->custom_color_callback = NULL;
77
/* id's 1 through 4 are reserved for Device color spaces; see gscspace.h */
78
pio->gs_next_id = 5; /* this implies that each thread has its own complete state */
80
pio->dict_auto_expand = false;
85
/* Provide a single point for all "C" stdout and stderr.
88
int outwrite(const gs_memory_t *mem, const char *str, int len)
92
gs_lib_ctx_t *pio = mem->gs_lib_ctx;
96
if (pio->stdout_is_redirected) {
97
if (pio->stdout_to_stderr)
98
return errwrite(str, len);
101
else if (pio->stdout_fn) {
102
return (*pio->stdout_fn)(pio->caller_handle, str, len);
107
code = fwrite(str, 1, len, fout);
112
int errwrite(const char *str, int len)
117
if (mem_err_print->gs_lib_ctx->stderr_fn)
118
return (*mem_err_print->gs_lib_ctx->stderr_fn)(mem_err_print->gs_lib_ctx->caller_handle, str, len);
120
code = fwrite(str, 1, len, mem_err_print->gs_lib_ctx->fstderr);
121
fflush(mem_err_print->gs_lib_ctx->fstderr);
125
void outflush(const gs_memory_t *mem)
127
if (mem->gs_lib_ctx->stdout_is_redirected) {
128
if (mem->gs_lib_ctx->stdout_to_stderr) {
129
if (!mem->gs_lib_ctx->stderr_fn)
130
fflush(mem->gs_lib_ctx->fstderr);
133
fflush(mem->gs_lib_ctx->fstdout2);
135
else if (!mem->gs_lib_ctx->stdout_fn)
136
fflush(mem->gs_lib_ctx->fstdout);
141
if (!mem_err_print->gs_lib_ctx->stderr_fn)
142
fflush(mem_err_print->gs_lib_ctx->fstderr);
143
/* else nothing to flush */