files.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* Copyright (C) 1989, 1995, 1996, 1997, 1998, 1999 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: files.h,v 1.10 2004/08/04 19:36:12 stefan Exp $ */
  14. /* Definitions for interpreter support for file objects */
  15. /* Requires stream.h */
  16. #ifndef files_INCLUDED
  17. # define files_INCLUDED
  18. /*
  19. * File objects store a pointer to a stream in value.pfile.
  20. * A file object is valid if its "size" matches the read_id or write_id
  21. * (as appropriate) in the stream it points to. This arrangement
  22. * allows us to detect closed files reliably, while allowing us to
  23. * reuse closed streams for new files.
  24. */
  25. #define fptr(pref) (pref)->value.pfile
  26. #define make_file(pref,a,id,s)\
  27. make_tasv(pref,t_file,a,id,pfile,s)
  28. /* The stdxxx files. We have to access them through procedures, */
  29. /* because they might have to be opened when referenced. */
  30. int zget_stdin(i_ctx_t *, stream **);
  31. int zget_stdout(i_ctx_t *, stream **);
  32. int zget_stderr(i_ctx_t *, stream **);
  33. /* Test whether a stream is stdin. */
  34. bool zis_stdin(const stream *);
  35. /* Define access to the stdio refs for operators. */
  36. #define ref_stdio (i_ctx_p->stdio)
  37. #define ref_stdin ref_stdio[0]
  38. #define ref_stdout ref_stdio[1]
  39. #define ref_stderr ref_stdio[2]
  40. /* An invalid (closed) file. */
  41. #define avm_invalid_file_entry avm_foreign
  42. extern stream *const invalid_file_entry;
  43. /* Make an invalid file object. */
  44. void make_invalid_file(ref *);
  45. /*
  46. * Macros for checking file validity.
  47. * NOTE: in order to work around a bug in the Borland 5.0 compiler,
  48. * you must use file_is_invalid rather than !file_is_valid.
  49. */
  50. #define file_is_valid(svar,op)\
  51. (svar = fptr(op), (svar->read_id | svar->write_id) == r_size(op))
  52. #define file_is_invalid(svar,op)\
  53. (svar = fptr(op), (svar->read_id | svar->write_id) != r_size(op))
  54. #define check_file(svar,op)\
  55. BEGIN\
  56. check_type(*(op), t_file);\
  57. if ( file_is_invalid(svar, op) ) return_error(e_invalidaccess);\
  58. END
  59. /*
  60. * If a file is open for both reading and writing, its read_id, write_id,
  61. * and stream procedures and modes reflect the current mode of use;
  62. * an id check failure will switch it to the other mode.
  63. */
  64. int file_switch_to_read(const ref *);
  65. #define check_read_file(svar,op)\
  66. BEGIN\
  67. check_read_type(*(op), t_file);\
  68. check_read_known_file(svar, op, return);\
  69. END
  70. #define check_read_known_file(svar,op,error_return)\
  71. check_read_known_file_else(svar, op, error_return, svar = invalid_file_entry)
  72. #define check_read_known_file_else(svar,op,error_return,invalid_action)\
  73. BEGIN\
  74. svar = fptr(op);\
  75. if (svar->read_id != r_size(op)) {\
  76. if (svar->read_id == 0 && svar->write_id == r_size(op)) {\
  77. int fcode = file_switch_to_read(op);\
  78. \
  79. if (fcode < 0)\
  80. error_return(fcode);\
  81. } else {\
  82. invalid_action; /* closed or reopened file */\
  83. }\
  84. }\
  85. END
  86. int file_switch_to_write(const ref *);
  87. #define check_write_file(svar,op)\
  88. BEGIN\
  89. check_write_type(*(op), t_file);\
  90. check_write_known_file(svar, op, return);\
  91. END
  92. #define check_write_known_file(svar,op,error_return)\
  93. BEGIN\
  94. svar = fptr(op);\
  95. if ( svar->write_id != r_size(op) )\
  96. { int fcode = file_switch_to_write(op);\
  97. if ( fcode < 0 ) error_return(fcode);\
  98. }\
  99. END
  100. /* Data exported by zfile.c. */
  101. /* for zfilter.c and ziodev.c */
  102. extern const uint file_default_buffer_size;
  103. #ifndef gs_file_path_ptr_DEFINED
  104. # define gs_file_path_ptr_DEFINED
  105. typedef struct gs_file_path_s *gs_file_path_ptr;
  106. #endif
  107. /* Procedures exported by zfile.c. */
  108. /* for imainarg.c */
  109. FILE *lib_fopen(const gs_file_path_ptr pfpath, const gs_memory_t *mem, const char *);
  110. /* for imain.c */
  111. int lib_file_open(const gs_file_path_ptr pfpath, i_ctx_t *, const char *, uint, byte *, uint,
  112. uint *, ref *, gs_memory_t *);
  113. /* for imain.c */
  114. #ifndef gs_ref_memory_DEFINED
  115. # define gs_ref_memory_DEFINED
  116. typedef struct gs_ref_memory_s gs_ref_memory_t;
  117. #endif
  118. int file_read_string(const byte *, uint, ref *, gs_ref_memory_t *);
  119. /* for os_open in ziodev.c */
  120. #ifdef iodev_proc_fopen /* in gxiodev.h */
  121. int file_open_stream(const char *, uint, const char *, uint, stream **,
  122. gx_io_device *, iodev_proc_fopen_t, gs_memory_t *);
  123. #endif
  124. /* for zfilter.c */
  125. int filter_open(const char *, uint, ref *, const stream_procs *,
  126. const stream_template *, const stream_state *,
  127. gs_memory_t *);
  128. /* for zfileio.c */
  129. void make_stream_file(ref *, stream *, const char *);
  130. /* for ziodev.c */
  131. int file_close_finish(stream *);
  132. int file_close_disable(stream *);
  133. int file_close_file(stream *);
  134. /* for gsmain.c, interp.c */
  135. int file_close(ref *);
  136. /* for zfproc.c, ziodev.c */
  137. stream *file_alloc_stream(gs_memory_t *, client_name_t);
  138. /* Procedures exported by zfileio.c. */
  139. /* for ziodev.c */
  140. int zreadline_from(stream *s, gs_string *buf, gs_memory_t *bufmem,
  141. uint *pcount, bool *pin_eol);
  142. /* Procedures exported by zfileio.c. */
  143. /* for zfile.c */
  144. int zfilelineedit(i_ctx_t *i_ctx_p);
  145. /* for zfproc.c */
  146. int zneedstdin(i_ctx_t *i_ctx_p);
  147. int zneedstdout(i_ctx_t *i_ctx_p);
  148. int zneedstderr(i_ctx_t *i_ctx_p);
  149. #endif /* files_INCLUDED */