summaryrefslogtreecommitdiff
path: root/psi
diff options
context:
space:
mode:
authorKen Sharp <ken.sharp@artifex.com>2022-11-21 13:46:15 +0000
committerKen Sharp <ken.sharp@artifex.com>2022-11-21 16:34:12 +0000
commitbe2c3d0d315fe944368c5a5b83bb7f59fcb2a4ea (patch)
treec74428b92eccae2d95928b92e8cdf3711b264b35 /psi
parentc3f882ec0aef0a6a3094999a6b03ad093ee6ec04 (diff)
downloadghostpdl-be2c3d0d315fe944368c5a5b83bb7f59fcb2a4ea.tar.gz
Allow @file syntax to include files in the ROM file system
This started as a customer request, where the customer wanted to store a file in the ROM file system with a bunch of command line parameters in it, and then use the @file syntax to run that file and setup the interpreter. In essence a 'canned' setup stored in the ROM file system. That didn't work, at all, because of a dichotomy in the logic of argument processing. The code used 'lib_file_open' in order to find the file using the search path which, on Ghostscript, includes all the iodevices, Rom file system, RAM file system etc. That function returns a stream which the calling function 'lib_fopen' would then reach inside and pull out the pointer to the underlying gp__file *, and return, because the argument processing was written to use a gp_file *, not a stream *. But.... For the ROM file system, what we store in the 'file' member of the stream is not a gp_file *, its a 'node' *. Trying to use that as a gp_file * rapidly leads to a seg fault. So this commit reworks the argument processing to use a stream * instead of a gp_file *. In reality there is no real difference to the logic we simply use a different call. There are a lot of fiddly references to change unfortunately. There are some consequences; we need to pass stream * around instead of gp_file *, and in particular the 'encoding' functions, which are OS-specific need to use a stream *. This isn't a problem for the interpreters and graphics library, but mkromfs *also* uses those OS-specific files, and it does not have the stream library available. Rather than trying to include it, we note that we don't actually need to use these functions for mkromfs and we do the same hackery as for other missing functionality; we define a stub function that does nothing but will permit the compile to complete.
Diffstat (limited to 'psi')
-rw-r--r--psi/files.h4
-rw-r--r--psi/imainarg.c8
-rw-r--r--psi/imainarg.h4
-rw-r--r--psi/iminst.h4
-rw-r--r--psi/psapi.c16
-rw-r--r--psi/zfile.c18
6 files changed, 21 insertions, 33 deletions
diff --git a/psi/files.h b/psi/files.h
index 68d3395c6..7ad161b81 100644
--- a/psi/files.h
+++ b/psi/files.h
@@ -1,4 +1,4 @@
-/* Copyright (C) 2001-2021 Artifex Software, Inc.
+/* Copyright (C) 2001-2022 Artifex Software, Inc.
All Rights Reserved.
This software is provided AS-IS with no warranty, either express or
@@ -103,7 +103,7 @@ typedef struct gs_file_path_s *gs_file_path_ptr;
/* Procedures exported by zfile.c. */
/* for imainarg.c */
-gp_file *lib_fopen(const gs_file_path_ptr pfpath, const gs_memory_t *mem, const char *);
+stream *lib_sopen(const gs_file_path_ptr pfpath, const gs_memory_t *mem, const char *);
/* for imain.c */
int
diff --git a/psi/imainarg.c b/psi/imainarg.c
index aaf41b6bf..b3269e2e7 100644
--- a/psi/imainarg.c
+++ b/psi/imainarg.c
@@ -114,11 +114,11 @@ static void print_help_trailer(const gs_main_instance *);
/* ------ Main program ------ */
/* Process the command line with a given instance. */
-static gp_file *
-gs_main_arg_fopen(const char *fname, void *vminst)
+static stream *
+gs_main_arg_sopen(const char *fname, void *vminst)
{
gs_main_set_lib_paths((gs_main_instance *) vminst);
- return lib_fopen(&((gs_main_instance *)vminst)->lib_path,
+ return lib_sopen(&((gs_main_instance *)vminst)->lib_path,
((gs_main_instance *)vminst)->heap, fname);
}
static void
@@ -140,7 +140,7 @@ gs_main_init_with_args01(gs_main_instance * minst, int argc, char *argv[])
/* Now we actually process them */
code = arg_init(&args, (const char **)argv, argc,
- gs_main_arg_fopen, (void *)minst,
+ gs_main_arg_sopen, (void *)minst,
minst->get_codepoint,
minst->heap);
if (code < 0)
diff --git a/psi/imainarg.h b/psi/imainarg.h
index 7e330b901..fe94a77e9 100644
--- a/psi/imainarg.h
+++ b/psi/imainarg.h
@@ -1,4 +1,4 @@
-/* Copyright (C) 2001-2021 Artifex Software, Inc.
+/* Copyright (C) 2001-2022 Artifex Software, Inc.
All Rights Reserved.
This software is provided AS-IS with no warranty, either express or
@@ -49,7 +49,7 @@ int gs_main_run_start(gs_main_instance * minst);
* Allocation should be done with the standard gs allocation functions, as
* the returned string may be freed using the same.
*/
-typedef int (gs_arg_get_codepoint)(gp_file *, const char **);
+typedef int (gs_arg_get_codepoint)(stream *, const char **);
void gs_main_inst_arg_decode(gs_main_instance * minst,
gs_arg_get_codepoint *get_codepoint);
diff --git a/psi/iminst.h b/psi/iminst.h
index 4e71056d2..b249def51 100644
--- a/psi/iminst.h
+++ b/psi/iminst.h
@@ -1,4 +1,4 @@
-/* Copyright (C) 2001-2021 Artifex Software, Inc.
+/* Copyright (C) 2001-2022 Artifex Software, Inc.
All Rights Reserved.
This software is provided AS-IS with no warranty, either express or
@@ -77,7 +77,7 @@ struct gs_main_instance_s {
long base_time[2]; /* starting usertime */
void *readline_data; /* data for gp_readline */
ref error_object; /* Use by gsapi_*() */
- int (*get_codepoint)(gp_file *file, const char **astr);
+ int (*get_codepoint)(stream *s, const char **astr);
/* Get next 'unicode' codepoint */
display_callback *display; /* callback structure for display device */
/* The following are updated dynamically. */
diff --git a/psi/psapi.c b/psi/psapi.c
index 9321f34a9..454aef1e9 100644
--- a/psi/psapi.c
+++ b/psi/psapi.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 2001-2021 Artifex Software, Inc.
+/* Copyright (C) 2001-2022 Artifex Software, Inc.
All Rights Reserved.
This software is provided AS-IS with no warranty, either express or
@@ -165,7 +165,7 @@ psapi_delete_instance(gs_lib_ctx_t *ctx)
--gsapi_instance_counter;
}
-static int utf16le_get_codepoint(gp_file *file, const char **astr)
+static int utf16le_get_codepoint(stream *s, const char **astr)
{
int c;
int rune;
@@ -181,11 +181,11 @@ static int utf16le_get_codepoint(gp_file *file, const char **astr)
* be wrong. */
do {
- if (file) {
- rune = gp_fgetc(file);
+ if (s) {
+ rune = spgetc(s);
if (rune == EOF)
return EOF;
- c = gp_fgetc(file);
+ c = spgetc(s);
if (c == EOF)
return EOF;
rune += c<<8;
@@ -207,11 +207,11 @@ static int utf16le_get_codepoint(gp_file *file, const char **astr)
lead: /* We've just read a leading surrogate */
rune -= 0xD800;
rune <<= 10;
- if (file) {
- trail = gp_fgetc(file);
+ if (s) {
+ trail = spgetc(s);
if (trail == EOF)
return EOF;
- c = gp_fgetc(file);
+ c = spgetc(s);
if (c == EOF)
return EOF;
trail += c<<8;
diff --git a/psi/zfile.c b/psi/zfile.c
index e4a2a3e17..b8a17c8a7 100644
--- a/psi/zfile.c
+++ b/psi/zfile.c
@@ -1283,8 +1283,8 @@ lib_file_open(gs_file_path_ptr lib_path, const gs_memory_t *mem, i_ctx_t *i_ctx
}
/* The startup code calls this to open @-files. */
-gp_file *
-lib_fopen(const gs_file_path_ptr pfpath, const gs_memory_t *mem, const char *fname)
+stream *
+lib_sopen(const gs_file_path_ptr pfpath, const gs_memory_t *mem, const char *fname)
{
/* We need a buffer to hold the expanded file name. */
char filename_found[DEFAULT_BUFFER_SIZE];
@@ -1301,19 +1301,7 @@ lib_fopen(const gs_file_path_ptr pfpath, const gs_memory_t *mem, const char *fna
if (code < 0)
return NULL;
- /* This all seems a bit grotty. The above code has generated us a stream
- * that wraps a file. We actually want the file. So we reach in, and steal
- * the file pointer. Nasty. */
- s = ((stream *)(obj.value.pfile));
- file = s->file;
- /* Historically we've then just abandoned the stream, resulting in blocks
- * leaking. Let's free the leaked blocks (in rather hacky style). First,
- * we clear the file reference, and then drop the stream. */
- s->file = NULL;
- sclose(s);
- /* Then free the stream block itself. */
- gs_free_object(s->memory, s, "lib_fopen");
- return file;
+ return((stream *)(obj.value.pfile));
}
/* Open a file stream that reads a string. */