File: zfile.c

package info (click to toggle)
gs 3.33-7
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 7,436 kB
  • ctags: 15,511
  • sloc: ansic: 92,150; asm: 684; sh: 486; makefile: 91
file content (855 lines) | stat: -rw-r--r-- 25,055 bytes parent folder | download
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
/* Copyright (C) 1989, 1995 Aladdin Enterprises.  All rights reserved.
  
  This file is part of GNU Ghostscript.
  
  GNU Ghostscript is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility to
  anyone for the consequences of using it or for whether it serves any
  particular purpose or works at all, unless he says so in writing.  Refer
  to the GNU Ghostscript General Public License for full details.
  
*/

/* zfile.c */
/* Non-I/O file operators */
#include "memory_.h"
#include "string_.h"
#include "ghost.h"
#include "gp.h"
#include "gsstruct.h"			/* for registering root */
#include "errors.h"
#include "oper.h"
#include "estack.h"			/* for filenameforall, .runexec */
#include "ialloc.h"
#include "ilevel.h"			/* %names only work in Level 2 */
#include "interp.h"			/* gs_errorinfo_put_string prototype */
#include "isave.h"			/* for restore */
#include "iutil.h"
#include "stream.h"
#include "strimpl.h"
#include "sfilter.h"
#include "gxiodev.h"			/* must come after stream.h */
					/* and before files.h */
#include "files.h"			/* ditto */
#include "fname.h"			/* ditto */
#include "store.h"

/* Import the file_open routine for %os%, which is the default. */
extern iodev_proc_open_file(iodev_os_open_file);

/* Forward references: file opening. */
int file_open(P6(const byte *, uint, const char *, uint, ref *, stream **));

/* Forward references: other. */
private int runexec_finish(P1(os_ptr));
private int runexec_cleanup(P1(os_ptr));
private stream_proc_report_error(filter_report_error);

/* Imported from gs.c */
extern const char **gs_lib_paths;	/* search path list, */
					/* terminated by a null pointer */

/*
 * Since there can be many file objects referring to the same file/stream,
 * we can't simply free a stream when we close it.  On the other hand,
 * we don't want freed streams to clutter up memory needlessly.
 * Our solution is to retain the freed streams, and reuse them.
 * To prevent an old file object from being able to access a reused stream,
 * we keep a serial number in each stream, and check it against a serial
 * number stored in the file object (as the "size"); when we close a file,
 * we increment its serial number.  If the serial number ever overflows,
 * we leave it at zero, and do not reuse the stream.
 * (This will never happen.)
 *
 * Storage management for this scheme is a little tricky.
 * We maintain an invariant that says that a stream opened at a given
 * save level always uses a stream structure allocated at that level.
 * By doing this, we don't need to keep track separately of streams open
 * at a level vs. streams allocated at a level.  To make this interact
 * properly with save and restore, we maintain a list of all streams
 * currently allocated, both open and closed.  The save_count member
 * of a stream indicates the number of unmatched saves at which
 * the given stream was the head of the list.  Thus the streams
 * allocated at the current level are precisely those from the head of the
 * list up to and not including the first stream with non-zero save_count.
 *
 * We want to close streams freed by restore and by garbage collection.
 * We use the finalization procedure for this.  For restore, we don't
 * have to do anything special to make this happen; we only need to ensure
 * that we remove from the list of allocated streams any streams being freed,
 * as just described.  For garbage collection, we do something more drastic:
 * we simply clear the list of known streams (at all save levels).
 * Any streams open at the time of garbage collection will no longer
 * participate in the list of known streams, but this does no harm;
 * it simply means that they won't get reused, and can only be reclaimed
 * by a future garbage collection or restore.
 */
private stream *file_list;
private gs_gc_root_t file_list_root;

/* 
 * Define the default stream buffer sizes.  For file streams,
 * this is arbitrary, since the C library or operating system
 * does its own buffering in addition.
 * However, the buffer size for eexec decoding is NOT arbitrary:
 * it must be at most 512.
 */
#define default_buffer_size 512
const uint file_default_buffer_size = default_buffer_size;

/* An invalid file object */
stream *invalid_file_entry;		/* exported for zfileio.c */
private gs_gc_root_t invalid_file_root;

/* Initialize the file table */
private void
zfile_init(void)
{
	/* Create and initialize an invalid (closed) stream. */
	/* Initialize the stream for the sake of the GC, */
	/* and so it can act as an empty input stream. */

	stream *s = s_alloc(imemory_system, "zfile_init");
	sread_string(s, NULL, 0);
	s->next = s->prev = 0;
	s->save_count = 0;
	s_init_no_id(s);
	invalid_file_entry = s;
	gs_register_struct_root(imemory, &invalid_file_root,
				(void **)&invalid_file_entry,
				"invalid_file_entry");

	/* Initialize the bookkeeping list. */
	
	file_list = 0;
	gs_register_struct_root(imemory, &file_list_root,
				(void **)&file_list, "file_list");
}

/* Make an invalid file object. */
void
make_invalid_file(ref *fp)
{	make_file(fp, avm_system, ~0, invalid_file_entry);
}

/* <name_string> <access_string> file <file> */
int
zfile(register os_ptr op)
{	char file_access[3];
	parsed_file_name pname;
	const byte *astr;
	int code;
	stream *s;
	check_read_type(*op, t_string);
	astr = op->value.const_bytes;
	switch ( r_size(op) )
	   {
	case 2:
		if ( astr[1] != '+' )
			return_error(e_invalidfileaccess);
		file_access[1] = '+';
		file_access[2] = 0;
		break;
	case 1:
		file_access[1] = 0;
		break;
	default:
		return_error(e_invalidfileaccess);
	   }
	switch ( astr[0] )
	   {
	case 'r': case 'w': case 'a':
		break;
	default:
		return_error(e_invalidfileaccess);
	   }
	file_access[0] = astr[0];
	code = parse_file_name(op - 1, &pname);
	if ( code < 0 )
		return code;
	if ( pname.iodev == NULL )
		pname.iodev = iodev_default;
	if ( pname.fname == NULL )		/* just a device */
		code = (*pname.iodev->procs.open_device)(pname.iodev,
					file_access, &s, imemory);
	else					/* file */
	{	iodev_proc_open_file((*open_file)) =
			pname.iodev->procs.open_file;
		if ( open_file == 0 )
			open_file = iodev_os_open_file;
		code = (*open_file)(pname.iodev, pname.fname, pname.len,
				    file_access, &s, imemory);
	}
	if ( code < 0 )
		return code;
	make_stream_file(op - 1, s, file_access);
	pop(1);
	return code;
}

/* ------ Level 2 extensions ------ */

/* <string> deletefile - */
private int
zdeletefile(register os_ptr op)
{	parsed_file_name pname;
	int code = parse_real_file_name(op, &pname, "deletefile");
	if ( code < 0 )
		return code;
	code = (*pname.iodev->procs.delete_file)(pname.iodev, pname.fname);
	free_file_name(&pname, "deletefile");
	if ( code < 0 )
		return code;
	pop(1);
	return 0;
}

/* <template> <proc> <scratch> filenameforall - */
/****** NOT CONVERTED FOR IODEVICES YET ******/
private int file_continue(P1(os_ptr));
private int file_cleanup(P1(os_ptr));
private int
zfilenameforall(register os_ptr op)
{	file_enum *pfen;
	int code;
	check_write_type(*op, t_string);
	check_proc(op[-1]);
	check_read_type(op[-2], t_string);
	/* Push a mark, the pattern, the scratch string, the enumerator, */
	/* and the procedure, and invoke the continuation. */
	check_estack(7);
	pfen = gp_enumerate_files_init((char *)op[-2].value.bytes, r_size(op - 2), imemory);
	if ( pfen == 0 )
	  return_error(e_VMerror);
	push_mark_estack(es_for, file_cleanup);
	*++esp = op[-2];
	*++esp = *op;
	++esp;
	make_istruct(esp, 0, pfen);
	*++esp = op[-1];
	pop(3);  op -= 3;
	code = file_continue(op);
	return (code == o_pop_estack ? o_push_estack : code);
}
/* Continuation operator for enumerating files */
private int
file_continue(register os_ptr op)
{	es_ptr pscratch = esp - 2;
	file_enum *pfen = r_ptr(esp - 1, file_enum);
	uint len = r_size(pscratch);
	uint code =
	  gp_enumerate_files_next(pfen, (char *)pscratch->value.bytes, len);
	if ( code == ~(uint)0 )		/* all done */
	   {	esp -= 4;		/* pop proc, pfen, scratch, mark */
		return o_pop_estack;
	   }
	else if ( code > len )		/* overran string */
		return_error(e_rangecheck);
	else
	   {	push(1);
		ref_assign(op, pscratch);
		r_set_size(op, code);
		push_op_estack(file_continue);	/* come again */
		*++esp = pscratch[2];	/* proc */
		return o_push_estack;
	   }
}
/* Cleanup procedure for enumerating files */
private int
file_cleanup(os_ptr op)
{	gp_enumerate_files_close(r_ptr(esp + 4, file_enum));
	return 0;
}

/* <string1> <string2> renamefile - */
private int
zrenamefile(register os_ptr op)
{	parsed_file_name pname1, pname2;
	int code = parse_real_file_name(op - 1, &pname1, "renamefile(from)");
	if ( code < 0 )
		return code;
	pname2.fname = 0;
	code = parse_real_file_name(op, &pname2, "renamefile(to)");
	if ( code < 0 || pname1.iodev != pname2.iodev ||
	     (code = (*pname1.iodev->procs.rename_file)(pname1.iodev,
				     pname1.fname, pname2.fname)) < 0
	   )
	{	if ( code >= 0 )
			code = gs_note_error(e_invalidfileaccess);
	}
	free_file_name(&pname2, "renamefile(to)");
	free_file_name(&pname1, "renamefile(from)");
	if ( code < 0 )
		return code;
	pop(2);
	return 0;
}	

/* <file> status <open_bool> */
/* <string> status <pages> <bytes> <ref_time> <creation_time> true */
/* <string> status false */
private int
zstatus(register os_ptr op)
{	switch ( r_type(op) )
	{
	case t_file:
		make_bool(op, (s_is_valid(fptr(op)) ? 1 : 0));
		return 0;
	case t_string:
	{	parsed_file_name pname;
		struct stat fstat;
		int code = parse_file_name(op, &pname);
		if ( code < 0 )
			return code;
		code = terminate_file_name(&pname, "status");
		if ( code < 0 )
			return code;
		code = (*pname.iodev->procs.file_status)(pname.iodev,
				pname.fname, &fstat);
		switch ( code )
		{
		case 0:
			push(4);
			make_int(op - 4, stat_blocks(&fstat));
			make_int(op - 3, fstat.st_size);
			make_int(op - 2, fstat.st_mtime);
			make_int(op - 1, fstat.st_ctime);
			make_bool(op, 1);
			break;
		case e_undefinedfilename:
			make_bool(op, 0);
			code = 0;
		}
		free_file_name(&pname, "status");
		return code;
	}
	default:
		return_op_typecheck(op);
	}
}

/* ------ Non-standard extensions ------ */

/* <string> findlibfile <found_string> <file> true */
/* <string> findlibfile <string> false */
int
zfindlibfile(register os_ptr op)
{	int code;
#define maxclen 200
	byte cname[maxclen];
	uint clen;
	parsed_file_name pname;
	stream *s;
	check_ostack(2);
	code = parse_file_name(op, &pname);
	if ( code < 0 )
		return code;
	if ( pname.iodev == NULL )
		pname.iodev = iodev_default;
	if ( pname.iodev != iodev_default )
	{	/* Non-OS devices don't have search paths (yet). */
		code = (*pname.iodev->procs.open_file)(pname.iodev,
					pname.fname, pname.len, "r", &s,
					imemory);
		if ( code < 0 )
		{	push(1);
			make_false(op);
			return 0;
		}
		make_stream_file(op + 1, s, "r");
	}
	else
	{	byte *cstr;
		code = lib_file_open(pname.fname, pname.len, cname, maxclen,
				     &clen, op + 1);
		if ( code == e_VMerror )
		  return code;
		if ( code < 0 )
		{	push(1);
			make_false(op);
			return 0;
		}
		cstr = ialloc_string(clen, "findlibfile");
		if ( cstr == 0 )
			return_error(e_VMerror);
		memcpy(cstr, cname, clen);
		make_string(op, a_all | icurrent_space, clen, cstr);
	}
	push(2);
	make_true(op);
	return 0;
}

/* <executable_file> .runexec - */
private int
zrunexec(register os_ptr op)
{	check_type_access(*op, t_file, a_executable | a_read | a_execute);
	check_estack(4);	/* cleanup, file, finish, file */
	push_mark_estack(es_other, runexec_cleanup);
	*++esp = *op;
	push_op_estack(runexec_finish);
	return zexec(op);
}
/* Finish normally. */
private int
runexec_finish(os_ptr op)
{	check_ostack(1);
	esp -= 2;
	runexec_cleanup(op);
	return o_pop_estack;
}
/* Clean up by closing the file. */
private int
runexec_cleanup(os_ptr op)
{	check_ostack(1);
	*++osp = esp[2];
	return zclosefile(osp);
}

/* ------ Initialization procedure ------ */

BEGIN_OP_DEFS(zfile_op_defs) {
	{"1deletefile", zdeletefile},
	{"2file", zfile},
	{"3filenameforall", zfilenameforall},
	{"1findlibfile", zfindlibfile},
	{"2renamefile", zrenamefile},
	{"1.runexec", zrunexec},
	{"1status", zstatus},
		/* Internal operators */
	{"0%file_continue", file_continue},
	{"0%runexec_finish", runexec_finish},
END_OP_DEFS(zfile_init) }

/* ------ Stream opening ------ */

/* Make a t_file reference to a stream. */
void
make_stream_file(ref *pfile, stream *s, const char *access)
{	uint attrs =
		(access[1] == '+' ? a_write + a_read + a_execute : 0) |
		imemory_space((gs_ref_memory_t *)s->memory);
	if ( access[0] == 'r' )
	{	make_file(pfile, attrs | (a_read | a_execute), s->read_id, s);
		s->write_id = 0;
	}
	else
	{	make_file(pfile, attrs | a_write, s->write_id, s);
		s->read_id = 0;
	}
}

/* Open an OS-level file (like fopen), using the search paths if necessary. */
/* Note that it does not automatically look in the current */
/* directory first (or at all): this is like Unix, and unlike MS-DOS. */
private int
lib_file_fopen(gx_io_device *iodev, const char *bname,
  const char *ignore_access, FILE **pfile, char *rfname, uint rnamelen)
{	char fmode[3];			/* r, [b], null */
	int len = strlen(bname);
	const char **ppath;

	strcpy(fmode, "r");
	strcat(fmode, gp_fmode_binary_suffix);
	if ( gp_file_name_is_absolute(bname, len) )
	  return (*iodev->procs.fopen)(iodev, bname, fmode, pfile,
				       rfname, rnamelen);
	/* Go through the list of search paths */
	for ( ppath = gs_lib_paths; *ppath != 0; ppath++ )
	   {	const char *path = *ppath;

		for ( ; ; )
		   {	/* Find the end of the next path */
			const char *npath = path;
			uint plen;
			const char *cstr;
			int code;
			int up, i;

			while ( *npath != 0 && *npath != gp_file_name_list_separator )
			  npath++;
			plen = npath - path;
			cstr = gp_file_name_concat_string(path, plen, bname,
							  len);
			/* Concatenate the prefix, combiner, and file name. */
			/* Do this carefully in case rfname is the same */
			/* as fname.  (We don't worry about the case */
			/* where rfname only overlaps fname.) */
			up = plen + strlen(cstr);
			if ( up + len + 1 > rnamelen )
			  return_error(e_limitcheck);
			for ( i = len + 1; --i >= 0; )
			  rfname[i + up] = bname[i];
			memcpy(rfname, (byte *)path, plen);
			memcpy(rfname + plen, cstr, strlen(cstr));
			code = (*iodev->procs.fopen)(iodev, rfname, fmode,
						     pfile, rfname, rnamelen);
			if ( code >= 0 )
			  return code;
			/* strcpy isn't guaranteed to work for overlapping */
			/* source and destination, so: */
			if ( rfname == bname )
			  for ( i = 0; (rfname[i] = rfname[i + up]) != 0; i++ )
			    ;
			if ( !*npath ) break;
			path = npath + 1;
		   }
	   }
	return_error(e_undefinedfilename);
}
/* The startup code calls this to open @-files. */
FILE *
lib_fopen(const char *bname)
{	FILE *file = NULL;
	/* We need a buffer to hold the expanded file name. */
#define max_filename 129
	char buffer[max_filename];
	int code = lib_file_fopen(iodev_default, bname, "r", &file, 
				  buffer, max_filename);
#undef max_filename
	return (code < 0 ? NULL : file);
}

/* Open a file stream on an OS file and create a file object, */
/* using the search paths. */
/* The startup code calls this to open the initialization file gs_init.ps. */
int
lib_file_open(const char *fname, uint len, byte *cname, uint max_clen,
  uint *pclen, ref *pfile)
{	stream *s;
	int code = file_open_stream(fname, len, "r",
				    default_buffer_size, &s, lib_file_fopen);
	char *bname;
	uint blen;

	if ( code < 0 )
	  return code;
	/* Get the name from the stream buffer. */
	bname = (char *)s->cbuf;
	blen = strlen(bname);
	if ( blen > max_clen )
	{	sclose(s);
		return_error(e_limitcheck);
	}
	memcpy(cname, bname, blen);
	*pclen = blen;
	make_stream_file(pfile, s, "r");
	return 0;
}

/* Open a file stream that reads a string. */
/* (This is currently used only by gs_run_string and the ccinit feature.) */
/* The string must be allocated in non-garbage-collectable (foreign) space. */
int
file_read_string(const byte *str, uint len, stream *s, ref *pfile)
{	int space;
	if ( s == 0 )
	  {	s = file_alloc_stream(imemory, "file_read_string");
		if ( s == 0 )
		  return_error(e_VMerror);
		space = icurrent_space;
	  }
	else
	  {	s->memory = 0;
		s->report_error = s_no_report_error;
		s_init_ids(s);
		s->is_temp = 0;
		s->next = s->prev = 0;
		space = avm_foreign;
	  }
	sread_string(s, str, len);
	s->foreign = 1;
	s->write_id = 0;
	make_file(pfile, a_readonly | space, s->read_id, s);
	s->save_close = s->procs.close;
	s->procs.close = file_close_disable;
	return 0;
}

/* Open a file stream, optionally on an OS file. */
/* Return 0 if successful, error code if not. */
/* On a successful return, the C file name is in the stream buffer. */
/* If fname==0, set up the file entry, stream, and buffer, */
/* but don't open an OS file or initialize the stream. */
int
file_open_stream(const char *fname, uint len, const char *file_access,
  uint buffer_size, stream **ps, iodev_proc_fopen_t fopen_proc)
{	byte *buffer;
	register stream *s;
	if ( buffer_size == 0 )
	  buffer_size = default_buffer_size;
	if ( len >= buffer_size )
	  return_error(e_limitcheck);	/* we copy the file name into the buffer */
	/* Allocate the stream first, since it persists */
	/* even after the file has been closed. */
  	s = file_alloc_stream(imemory, "file_open_stream");
	if ( s == 0 )
		return_error(e_VMerror);
	/* Allocate the buffer. */
	buffer = ialloc_bytes(buffer_size, "file_open(buffer)");
	if ( buffer == 0 )
		return_error(e_VMerror);
	if ( fname != 0 )
	   {	/* Copy the name (so we can terminate it with a zero byte.) */
		char *file_name = (char *)buffer;
		char fmode[4];		/* r/w/a, [+], [b], null */
		FILE *file;
		int code;
		memcpy(file_name, fname, len);
		file_name[len] = 0;		/* terminate string */
		/* Open the file, always in binary mode. */
		strcpy(fmode, file_access);
		strcat(fmode, gp_fmode_binary_suffix);
		/****** iodev_default IS QUESTIONABLE ******/
		code = (*fopen_proc)(iodev_default, file_name, fmode, &file,
				     (char *)buffer, buffer_size);
		if (code < 0 )
		{	ifree_object(buffer, "file_open(buffer)");
			return code;
		}
		/* Set up the stream. */
		switch ( fmode[0] )
		{
		case 'a':
			sappend_file(s, file, buffer, buffer_size);
			break;
		case 'r':
			sread_file(s, file, buffer, buffer_size);
			break;
		case 'w':
			swrite_file(s, file, buffer, buffer_size);
		}
		if ( fmode[1] == '+' )
		  s->file_modes |= s_mode_read | s_mode_write;
		s->save_close = s->procs.close;
		s->procs.close = file_close_file;
	   }
	else				/* save the buffer and size */
	   {	s->cbuf = buffer;
		s->bsize = s->cbsize = buffer_size;
	   }
	*ps = s;
	return 0;
}

/* Open a file stream for a filter. */
int
filter_open(const char *file_access, uint buffer_size, ref *pfile,
  const stream_procs _ds *procs, const stream_template *template,
  const stream_state *st, stream **ps)
{	stream *s;
	uint ssize = gs_struct_type_size(template->stype);
	stream_state *sst = 0;
	int code;
	if ( template->stype != &st_stream_state )
	{	sst = s_alloc_state(imemory, template->stype,
				    "filter_open(stream_state)");
		if ( sst == 0 )
		  return_error(e_VMerror);
	}
	code = file_open_stream((char *)0, 0, file_access,
				buffer_size, &s, (iodev_proc_fopen_t)0);
	if ( code < 0 )
	{	ifree_object(sst, "filter_open(stream_state)");
		return code;
	}
	s_std_init(s, s->cbuf, s->bsize, procs,
		   (*file_access == 'r' ? s_mode_read : s_mode_write));
	make_stream_file(pfile, s, file_access);
	s->procs.process = template->process;
	s->save_close = s->procs.close;
	s->procs.close = file_close_file;
	if ( sst == 0 )
	{	/* This stream doesn't have any state of its own. */
		/* Hack: use the stream itself as the state. */
		sst = (stream_state *)s;
	}
	else if ( st != 0 )	/* might not have client parameters */
		memcpy(sst, st, ssize);
	s->state = sst;
	sst->template = template;
	sst->memory = imemory;
	sst->report_error = filter_report_error;
	if ( template->init != 0 )
	{	code = (*template->init)(sst);
		if ( code < 0 )
		{	ifree_object(sst, "filter_open(stream_state)");
			ifree_object(s->cbuf, "filter_open(buffer)");
			return code;
		}
	}
	*ps = s;
	return 0;
}

/* Report an error by storing it in $error.errorinfo. */
private int
filter_report_error(stream_state *st, const char *str)
{	if_debug1('s', "[s]stream error: %s\n", str);
	return gs_errorinfo_put_string(str);
}

/* Allocate and return a file stream. */
/* Return 0 if the allocation failed. */
/* The stream is initialized to an invalid state, so the caller need not */
/* worry about cleaning up if a later step in opening the stream fails. */
stream *
file_alloc_stream(gs_memory_t *mem, client_name_t cname)
{	stream *s;
	/* Look first for a free stream allocated at this level. */
	s = file_list;
	while ( s != 0 && s->save_count == 0 )
	{	if ( !s_is_valid(s) && s->read_id != 0 /* i.e. !overflowed */
		     && s->memory == mem
		   )
		{	s->is_temp = 0;		/* not a temp stream */
			return s;
		}
		s = s->next;
	}
	s = s_alloc(mem, cname);
	if ( s == 0 )
	  return 0;
	s_init_ids(s);
	s->is_temp = 0;			/* not a temp stream */
	/* Disable the stream now (in case we can't open the file, */
	/* or a filter init procedure fails) so that `restore' won't */
	/* crash when it tries to close open files. */
	s_disable(s);
	/* Add s to the list of files. */
	if ( file_list != 0 )
	  file_list->prev = s;
	s->next = file_list;
	s->prev = 0;
	s->save_count = 0;
	file_list = s;
	return s;
}

/* ------ Stream closing ------ */

/* Finish closing a file stream.  This used to check whether it was */
/* currentfile, but we don't have to do this any longer. */
/* This replaces the close procedure for the std* streams, */
/* which cannot actually be closed. */
/* This is exported for ziodev.c. */
int
file_close_finish(stream *s)
{	return 0;
}

/* Close a file stream, but don't deallocate the buffer. */
/* This replaces the close procedure for %lineedit and %statementedit. */
/* (This is WRONG: these streams should allocate a new buffer each time */
/* they are opened, but that would overstress the allocator right now.) */
/* This is exported for ziodev.c. */
/* This also replaces the close procedure for the string-reading */
/* stream created for gs_run_string. */
int
file_close_disable(stream *s)
{	int code = (*s->save_close)(s);
	if ( code )
	  return code;
	/* Increment the IDs to prevent further access. */
	s->read_id = s->write_id = (s->read_id | s->write_id) + 1;
	return file_close_finish(s);
}

/* Close a file stream.  This replaces the close procedure in the stream */
/* for normal (OS) files and for filters. */
int
file_close_file(stream *s)
{	stream *stemp = s->strm;
	gs_memory_t *mem;
	int code = file_close_disable(s);
	if ( code )
	  return code;
	/*
	 * Check for temporary streams created for filters.
	 * There may be more than one in the case of a procedure-based filter,
	 * or if we created an intermediate stream to ensure
	 * a large enough buffer.  Note that these streams may have been
	 * allocated by file_alloc_stream, so we mustn't free them.
	 */
	while ( stemp != 0 && stemp->is_temp != 0 )
	{	stream *snext = stemp->strm;
		mem = stemp->memory;
		if ( stemp->is_temp > 1 )
		 gs_free_object(mem, stemp->cbuf,
				"file_close(temp stream buffer)");
		s_disable(stemp);
		stemp = snext;
	}
	mem = s->memory;
	gs_free_object(mem, s->cbuf, "file_close(buffer)");
	return 0;
}

/* Close a file object. */
/* This is exported only for gsmain.c. */
int
file_close(ref *pfile)
{	stream *s;
	if ( file_is_valid(s, pfile) )	/* closing a closed file is a no-op */
	  {	if ( sclose(s) )
		  return_error(e_ioerror);
	  }
	return 0;
}

/* ------ Memory management ------ */

/* Mark the current file list at a save. */
void
file_save(void)
{	if_debug1('u', "[u]file_save 0x%lx\n",
		  (ulong)file_list);
	if ( file_list != 0 )
	  file_list->save_count++;
}

/* Update the file list before a restore. */
void
file_restore(const alloc_save_t *save, const gs_memory_t *mem)
{	stream *s;
	stream **ps;
	/* We must be careful to unlink only those streams which */
	/* were allocated in the VM space being restored. */
	for ( ps = &file_list; (s = *ps) != 0 && s->save_count == 0; )
	  if ( s->memory == mem )
	    *ps = s->next;
	  else
	    ps = &s->next;
	if ( s != 0 )
	  {	if ( s->prev )
		  s->prev->next = 0;
		s->prev = 0;
		s->save_count--;
	  }
	if_debug2('u', "[u]file_restore 0x%lx for 0x%lx\n",
		  (ulong)file_list, (ulong)save);
}

/* Note that a save has been forgotten. */
void
file_forget_save(const alloc_save_t *save)
{	stream *s;
	for ( s = file_list; s != 0 && s->save_count == 0; )
	  s = s->next;
	if ( s != 0 )
	  s->save_count--;
	if_debug2('u', "[u]file_forget_save 0x%lx for 0x%lx\n",
		  (ulong)file_list, (ulong)save);
}

/* Clear the file list for a GC. */
void
file_gc_prepare(void)
{	/* We have to unlink every stream from its neighbors, */
	/* so that referenced streams don't keep all streams around. */
	while ( file_list != 0 )
	  {	stream *s = file_list;
		file_list = s->next;
		s->prev = s->next = 0;
	  }
}