zfproc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /* Copyright (C) 1994, 1995, 1997, 1998, 1999, 2001 Aladdin Enterprises. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  4. distributor accepts any responsibility for the consequences of using it, or
  5. for whether it serves any particular purpose or works at all, unless he or
  6. she says so in writing. Refer to the Aladdin Free Public License (the
  7. "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. in a plain ASCII text file named PUBLIC. The License grants you the right
  10. to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. conditions described in the License. Among other things, the License
  12. requires that the copyright notice and this notice be preserved on all
  13. copies.
  14. */
  15. /*$Id: zfproc.c,v 1.9 2001/03/29 04:09:40 rayjj Exp $ */
  16. /* Procedure-based filter stream support */
  17. #include "memory_.h"
  18. #include "ghost.h"
  19. #include "oper.h" /* for ifilter.h */
  20. #include "estack.h"
  21. #include "gsstruct.h"
  22. #include "ialloc.h"
  23. #include "istruct.h" /* for RELOC_REF_VAR */
  24. #include "stream.h"
  25. #include "strimpl.h"
  26. #include "ifilter.h"
  27. #include "files.h"
  28. #include "store.h"
  29. /* ---------------- Generic ---------------- */
  30. /* GC procedures */
  31. private
  32. CLEAR_MARKS_PROC(sproc_clear_marks)
  33. {
  34. stream_proc_state *const pptr = vptr;
  35. r_clear_attrs(&pptr->proc, l_mark);
  36. r_clear_attrs(&pptr->data, l_mark);
  37. }
  38. private
  39. ENUM_PTRS_WITH(sproc_enum_ptrs, stream_proc_state *pptr) return 0;
  40. case 0:
  41. ENUM_RETURN_REF(&pptr->proc);
  42. case 1:
  43. ENUM_RETURN_REF(&pptr->data);
  44. ENUM_PTRS_END
  45. private RELOC_PTRS_WITH(sproc_reloc_ptrs, stream_proc_state *pptr);
  46. RELOC_REF_VAR(pptr->proc);
  47. r_clear_attrs(&pptr->proc, l_mark);
  48. RELOC_REF_VAR(pptr->data);
  49. r_clear_attrs(&pptr->data, l_mark);
  50. RELOC_PTRS_END
  51. /* Structure type for procedure-based streams. */
  52. private_st_stream_proc_state();
  53. /* Allocate and open a procedure-based filter. */
  54. /* The caller must have checked that *sop is a procedure. */
  55. private int
  56. s_proc_init(ref * sop, stream ** psstrm, uint mode,
  57. const stream_template * temp, const stream_procs * procs,
  58. gs_ref_memory_t *imem)
  59. {
  60. gs_memory_t *const mem = (gs_memory_t *)imem;
  61. stream *sstrm = file_alloc_stream(mem, "s_proc_init(stream)");
  62. stream_proc_state *state = (stream_proc_state *)
  63. s_alloc_state(mem, &st_sproc_state, "s_proc_init(state)");
  64. if (sstrm == 0 || state == 0) {
  65. gs_free_object(mem, state, "s_proc_init(state)");
  66. /*gs_free_object(mem, sstrm, "s_proc_init(stream)"); *//* just leave it on the file list */
  67. return_error(e_VMerror);
  68. }
  69. s_std_init(sstrm, NULL, 0, procs, mode);
  70. sstrm->procs.process = temp->process;
  71. state->template = temp;
  72. state->memory = mem;
  73. state->eof = 0;
  74. state->proc = *sop;
  75. make_empty_string(&state->data, a_all);
  76. state->index = 0;
  77. sstrm->state = (stream_state *) state;
  78. *psstrm = sstrm;
  79. return 0;
  80. }
  81. /* Handle an interrupt during a stream operation. */
  82. /* This is logically unrelated to procedure streams, */
  83. /* but it is also associated with the interpreter stream machinery. */
  84. private int
  85. s_handle_intc(i_ctx_t *i_ctx_p, const ref *pstate, int nstate,
  86. op_proc_t cont)
  87. {
  88. int npush = nstate + 2;
  89. check_estack(npush);
  90. if (nstate)
  91. memcpy(esp + 2, pstate, nstate * sizeof(ref));
  92. #if 0 /* **************** */
  93. {
  94. int code = gs_interpret_error(e_interrupt, (ref *) (esp + npush));
  95. if (code < 0)
  96. return code;
  97. }
  98. #else /* **************** */
  99. npush--;
  100. #endif /* **************** */
  101. make_op_estack(esp + 1, cont);
  102. esp += npush;
  103. return o_push_estack;
  104. }
  105. /* Set default parameter values (actually, just clear pointers). */
  106. private void
  107. s_proc_set_defaults(stream_state * st)
  108. {
  109. stream_proc_state *const ss = (stream_proc_state *) st;
  110. make_null(&ss->proc);
  111. make_null(&ss->data);
  112. }
  113. /* ---------------- Read streams ---------------- */
  114. /* Forward references */
  115. private stream_proc_process(s_proc_read_process);
  116. private int s_proc_read_continue(P1(i_ctx_t *));
  117. /* Stream templates */
  118. private const stream_template s_proc_read_template = {
  119. &st_sproc_state, NULL, s_proc_read_process, 1, 1,
  120. NULL, s_proc_set_defaults
  121. };
  122. private const stream_procs s_proc_read_procs = {
  123. s_std_noavailable, s_std_noseek, s_std_read_reset,
  124. s_std_read_flush, s_std_null, NULL
  125. };
  126. /* Allocate and open a procedure-based read stream. */
  127. /* The caller must have checked that *sop is a procedure. */
  128. int
  129. sread_proc(ref * sop, stream ** psstrm, gs_ref_memory_t *imem)
  130. {
  131. int code =
  132. s_proc_init(sop, psstrm, s_mode_read, &s_proc_read_template,
  133. &s_proc_read_procs, imem);
  134. if (code < 0)
  135. return code;
  136. (*psstrm)->end_status = CALLC;
  137. return code;
  138. }
  139. /* Handle an input request. */
  140. private int
  141. s_proc_read_process(stream_state * st, stream_cursor_read * ignore_pr,
  142. stream_cursor_write * pw, bool last)
  143. {
  144. /* Move data from the string returned by the procedure */
  145. /* into the stream buffer, or ask for a callback. */
  146. stream_proc_state *const ss = (stream_proc_state *) st;
  147. uint count = r_size(&ss->data) - ss->index;
  148. if (count > 0) {
  149. uint wcount = pw->limit - pw->ptr;
  150. if (wcount < count)
  151. count = wcount;
  152. memcpy(pw->ptr + 1, ss->data.value.bytes + ss->index, count);
  153. pw->ptr += count;
  154. ss->index += count;
  155. return 1;
  156. }
  157. return (ss->eof ? EOFC : CALLC);
  158. }
  159. /* Handle an exception (INTC or CALLC) from a read stream */
  160. /* whose buffer is empty. */
  161. int
  162. s_handle_read_exception(i_ctx_t *i_ctx_p, int status, const ref * fop,
  163. const ref * pstate, int nstate, op_proc_t cont)
  164. {
  165. int npush = nstate + 4;
  166. stream *ps;
  167. stream *psstdin;
  168. switch (status) {
  169. case INTC:
  170. return s_handle_intc(i_ctx_p, pstate, nstate, cont);
  171. case CALLC:
  172. break;
  173. default:
  174. return_error(e_ioerror);
  175. }
  176. /* Find the stream whose buffer needs refilling. */
  177. for (ps = fptr(fop); ps->strm != 0;)
  178. ps = ps->strm;
  179. check_estack(npush);
  180. if (nstate)
  181. memcpy(esp + 2, pstate, nstate * sizeof(ref));
  182. make_op_estack(esp + 1, cont);
  183. esp += npush;
  184. make_op_estack(esp - 2, s_proc_read_continue);
  185. esp[-1] = *fop;
  186. r_clear_attrs(esp - 1, a_executable);
  187. *esp = ((stream_proc_state *) ps->state)->proc;
  188. /* If stream is stdin, ask for callout. */
  189. zget_stdin(i_ctx_p, &psstdin);
  190. if (ps == psstdin) {
  191. check_estack(1);
  192. esp += 1;
  193. make_op_estack(esp, zneedstdin);
  194. }
  195. return o_push_estack;
  196. }
  197. /* Continue a read operation after returning from a procedure callout. */
  198. /* osp[0] contains the file (pushed on the e-stack by handle_read_status); */
  199. /* osp[-1] contains the new data string (pushed by the procedure). */
  200. /* The top of the e-stack contains the real continuation. */
  201. private int
  202. s_proc_read_continue(i_ctx_t *i_ctx_p)
  203. {
  204. os_ptr op = osp;
  205. os_ptr opbuf = op - 1;
  206. stream *ps;
  207. stream_proc_state *ss;
  208. check_file(ps, op);
  209. check_read_type(*opbuf, t_string);
  210. while ((ps->end_status = 0, ps->strm) != 0)
  211. ps = ps->strm;
  212. ss = (stream_proc_state *) ps->state;
  213. ss->data = *opbuf;
  214. ss->index = 0;
  215. if (r_size(opbuf) == 0)
  216. ss->eof = true;
  217. pop(2);
  218. return 0;
  219. }
  220. /* ---------------- Write streams ---------------- */
  221. /* Forward references */
  222. private stream_proc_flush(s_proc_write_flush);
  223. private stream_proc_process(s_proc_write_process);
  224. private int s_proc_write_continue(P1(i_ctx_t *));
  225. /* Stream templates */
  226. private const stream_template s_proc_write_template = {
  227. &st_sproc_state, NULL, s_proc_write_process, 1, 1,
  228. NULL, s_proc_set_defaults
  229. };
  230. private const stream_procs s_proc_write_procs = {
  231. s_std_noavailable, s_std_noseek, s_std_write_reset,
  232. s_proc_write_flush, s_std_null, NULL
  233. };
  234. /* Allocate and open a procedure-based write stream. */
  235. /* The caller must have checked that *sop is a procedure. */
  236. int
  237. swrite_proc(ref * sop, stream ** psstrm, gs_ref_memory_t *imem)
  238. {
  239. return s_proc_init(sop, psstrm, s_mode_write, &s_proc_write_template,
  240. &s_proc_write_procs, imem);
  241. }
  242. /* Handle an output request. */
  243. private int
  244. s_proc_write_process(stream_state * st, stream_cursor_read * pr,
  245. stream_cursor_write * ignore_pw, bool last)
  246. {
  247. /* Move data from the stream buffer to the string */
  248. /* returned by the procedure, or ask for a callback. */
  249. stream_proc_state *const ss = (stream_proc_state *) st;
  250. uint rcount = pr->limit - pr->ptr;
  251. if (rcount > 0) {
  252. uint wcount = r_size(&ss->data) - ss->index;
  253. uint count = min(rcount, wcount);
  254. memcpy(ss->data.value.bytes + ss->index, pr->ptr + 1, count);
  255. pr->ptr += count;
  256. ss->index += count;
  257. if (rcount > wcount)
  258. return CALLC;
  259. else if (last) {
  260. ss->eof = true;
  261. return CALLC;
  262. } else
  263. return 0;
  264. }
  265. return ((ss->eof = last) ? EOFC : 0);
  266. }
  267. /* Flush the output. This is non-standard because it must call the */
  268. /* procedure. */
  269. private int
  270. s_proc_write_flush(stream *s)
  271. {
  272. int result = s_process_write_buf(s, false);
  273. stream_proc_state *const ss = (stream_proc_state *)s->state;
  274. return (result < 0 || ss->index == 0 ? result : CALLC);
  275. }
  276. /* Handle an exception (INTC or CALLC) from a write stream */
  277. /* whose buffer is full. */
  278. int
  279. s_handle_write_exception(i_ctx_t *i_ctx_p, int status, const ref * fop,
  280. const ref * pstate, int nstate, op_proc_t cont)
  281. {
  282. stream *ps;
  283. stream *psstderr;
  284. stream *psstdout;
  285. stream_proc_state *psst;
  286. switch (status) {
  287. case INTC:
  288. return s_handle_intc(i_ctx_p, pstate, nstate, cont);
  289. case CALLC:
  290. break;
  291. default:
  292. return_error(e_ioerror);
  293. }
  294. /* Find the stream whose buffer needs emptying. */
  295. for (ps = fptr(fop); ps->strm != 0;)
  296. ps = ps->strm;
  297. psst = (stream_proc_state *) ps->state;
  298. {
  299. int npush = nstate + 6;
  300. check_estack(npush);
  301. if (nstate)
  302. memcpy(esp + 2, pstate, nstate * sizeof(ref));
  303. make_op_estack(esp + 1, cont);
  304. esp += npush;
  305. make_op_estack(esp - 4, s_proc_write_continue);
  306. esp[-3] = *fop;
  307. r_clear_attrs(esp - 3, a_executable);
  308. make_bool(esp - 1, !psst->eof);
  309. }
  310. esp[-2] = psst->proc;
  311. *esp = psst->data;
  312. r_set_size(esp, psst->index);
  313. /* If stream is stdout/err, ask for callout. */
  314. zget_stdout(i_ctx_p, &psstdout);
  315. zget_stderr(i_ctx_p, &psstderr);
  316. if ((ps == psstderr) || (ps == psstdout)) {
  317. check_estack(1);
  318. esp += 1;
  319. make_op_estack(esp, (ps == psstderr) ? zneedstderr : zneedstdout);
  320. }
  321. return o_push_estack;
  322. }
  323. /* Continue a write operation after returning from a procedure callout. */
  324. /* osp[0] contains the file (pushed on the e-stack by handle_write_status); */
  325. /* osp[-1] contains the new buffer string (pushed by the procedure). */
  326. /* The top of the e-stack contains the real continuation. */
  327. private int
  328. s_proc_write_continue(i_ctx_t *i_ctx_p)
  329. {
  330. os_ptr op = osp;
  331. os_ptr opbuf = op - 1;
  332. stream *ps;
  333. stream_proc_state *ss;
  334. check_file(ps, op);
  335. check_write_type(*opbuf, t_string);
  336. while (ps->strm != 0) {
  337. if (ps->end_status == CALLC)
  338. ps->end_status = 0;
  339. ps = ps->strm;
  340. }
  341. ps->end_status = 0;
  342. ss = (stream_proc_state *) ps->state;
  343. ss->data = *opbuf;
  344. ss->index = 0;
  345. pop(2);
  346. return 0;
  347. }
  348. /* ------ More generic ------ */
  349. /* Test whether a stream is procedure-based. */
  350. bool
  351. s_is_proc(const stream *s)
  352. {
  353. return (s->procs.process == s_proc_read_process ||
  354. s->procs.process == s_proc_write_process);
  355. }
  356. /* ------ Initialization procedure ------ */
  357. const op_def zfproc_op_defs[] =
  358. {
  359. /* Internal operators */
  360. {"2%s_proc_read_continue", s_proc_read_continue},
  361. {"2%s_proc_write_continue", s_proc_write_continue},
  362. op_def_end(0)
  363. };