home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / ZFPROC.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  8KB  |  284 lines

  1. /* Copyright (C) 1994 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* zfproc.c */
  20. /* Procedure-based filter stream support */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "oper.h"        /* for ifilter.h */
  24. #include "estack.h"
  25. #include "gsstruct.h"
  26. #include "ialloc.h"
  27. #include "istruct.h"        /* for gs_reloc_refs */
  28. #include "stream.h"
  29. #include "strimpl.h"
  30. #include "ifilter.h"
  31. #include "files.h"
  32. #include "store.h"
  33.  
  34. extern int zpop(P1(os_ptr));
  35.  
  36. /* ---------------- Generic ---------------- */
  37.  
  38. /* GC procedures */
  39. #define pproc (&((stream_proc_state *)vptr)->proc)
  40. private CLEAR_MARKS_PROC(sproc_clear_marks) {
  41.     r_clear_attrs(pproc, l_mark);
  42. }
  43. private ENUM_PTRS_BEGIN(sproc_enum_ptrs) return 0;
  44.     case 0:
  45.         *pep = pproc;
  46.         return ptr_ref_type;
  47. ENUM_PTRS_END
  48. private RELOC_PTRS_BEGIN(sproc_reloc_ptrs) ;
  49.     gs_reloc_refs((ref_packed *)pproc, (ref_packed *)(pproc + 1), gcst);
  50.     r_clear_attrs(pproc, l_mark);
  51. RELOC_PTRS_END
  52. #undef pproc
  53. /* Structure type for procedure-based streams. */
  54. private_st_stream_proc_state();
  55.  
  56. /* Allocate and open a procedure-based stream. */
  57. private int
  58. s_proc_init(ref *sop, stream **psstrm, uint mode, const stream_template *temp)
  59. {    stream *sstrm;
  60.     stream_proc_state *state;
  61.     static const stream_procs procs =
  62.        {    s_std_noavailable, s_std_noseek, s_std_read_reset,
  63.         s_std_read_flush, s_std_null, NULL
  64.        };
  65.     check_proc(*sop);
  66.     sstrm = s_alloc(imemory, "s_proc_init(stream)");
  67.     state = (stream_proc_state *)s_alloc_state(imemory, &st_sproc_state,
  68.                            "s_proc_init(state)");
  69.     if ( sstrm == 0 || state == 0 )
  70.     {    ifree_object(state, "s_proc_init(state)");
  71.         ifree_object(sstrm, "s_proc_init(stream)");
  72.         return_error(e_VMerror);
  73.     }
  74.     s_std_init(sstrm, NULL, 0, &procs, mode);
  75.     sstrm->procs.process = temp->process;
  76.     state->template = temp;
  77.     state->memory = imemory;
  78.     state->eof = 0;
  79.     state->proc = *sop;
  80.     sstrm->state = (stream_state *)state;
  81.     *psstrm = sstrm;
  82.     return 0;
  83. }
  84.  
  85. /* Handle an interrupt during a stream operation. */
  86. /* This is logically unrelated to procedure streams, */
  87. /* but it is also associated with the interpreter stream machinery. */
  88. int
  89. s_handle_intc(const ref *pstate, int (*cont)(P1(os_ptr)))
  90. {    int npush;
  91.     if ( pstate )
  92.     {    check_estack(3);
  93.         ref_assign(esp + 2, pstate);
  94.         npush = 3;
  95.     }
  96.     else
  97.     {    check_estack(2);
  98.         npush = 2;
  99.     }
  100. #if 0                /* **************** */
  101.     { int code = gs_interpret_error(e_interrupt, (ref *)(esp + npush));
  102.       if ( code < 0 )
  103.         return code;
  104.     }
  105. #else                /* **************** */
  106.     npush--;
  107. #endif                /* **************** */
  108.     make_op_estack(esp + 1, cont);
  109.     esp += npush;
  110.     return o_push_estack;
  111. }
  112.  
  113.  
  114. /* ---------------- Read streams ---------------- */
  115.  
  116. /* Forward references */
  117. private stream_proc_process(s_proc_read_process);
  118. private int s_proc_read_continue(P1(os_ptr));
  119.  
  120. /* Stream templates */
  121. private const stream_template s_proc_read_template =
  122. {    &st_sproc_state, NULL, s_proc_read_process, 0, 0, NULL
  123. };
  124.  
  125. /* Allocate and open a procedure-based read stream. */
  126. int
  127. sread_proc(ref *sop, stream **psstrm)
  128. {    return s_proc_init(sop, psstrm, s_mode_read, &s_proc_read_template);
  129. }
  130.  
  131. /* Handle an input request. */
  132. #define ss ((stream_proc_state *)st)
  133. private int
  134. s_proc_read_process(stream_state *st, stream_cursor_read *ignore_pr,
  135.   stream_cursor_write *pw, bool last)
  136. {    return (ss->eof ? EOFC : CALLC);
  137. }
  138. #undef ss
  139.  
  140. /* Call the procedure when the buffer is empty. */
  141. int
  142. s_proc_read_call(stream *s, const ref *fop, const ref *pstate,
  143.   int (*cont)(P1(os_ptr)))
  144. {    int npush;
  145.     stream *ps = s;
  146.     /* Find the stream whose buffer needs refilling. */
  147.     while ( ps->strm != 0 )
  148.       ps = ps->strm;
  149. #define psst ((stream_proc_state *)ps->state)
  150.     if ( pstate )
  151.     {    check_estack(5);
  152.         esp[2] = *pstate;
  153.         npush = 5;
  154.     }
  155.     else
  156.     {    check_estack(4);
  157.         npush = 4;
  158.     }
  159.     make_op_estack(esp + 1, cont);
  160.     esp += npush;
  161.     make_op_estack(esp - 2, s_proc_read_continue);
  162.     esp[-1] = *fop;
  163.     r_clear_attrs(esp - 1, a_executable);
  164.     *esp = psst->proc;
  165. #undef psst
  166.     return o_push_estack;
  167. }
  168. /* Continue a read operation after returning from a procedure callout. */
  169. /* osp[0] contains the file (pushed on the e-stack by handle_read_status); */
  170. /* osp[-1] contains the new data string (pushed by the procedure). */
  171. /* The top of the e-stack contains the real continuation. */
  172. private int
  173. s_proc_read_continue(os_ptr op)
  174. {    os_ptr pbuf = op - 1;
  175.     stream *ps;
  176.     check_file(ps, op);
  177.     check_read_type(*pbuf, t_string);
  178.     while ( (ps->end_status = 0, ps->strm) != 0 )
  179.       ps = ps->strm;
  180.     ps->srptr = (ps->cbuf = pbuf->value.bytes) - 1;
  181.     ps->srlimit = ps->srptr + r_size(pbuf);
  182.     if ( r_size(pbuf) == 0 )
  183.       ((stream_proc_state *)ps->state)->eof = true;
  184.     pop(2);
  185.     return 0;
  186. }
  187.  
  188. /* ---------------- Write streams ---------------- */
  189.  
  190. /* Forward references */
  191. private stream_proc_process(s_proc_write_process);
  192. private int s_proc_write_continue(P1(os_ptr));
  193.  
  194. /* Stream templates */
  195. private const stream_template s_proc_write_template =
  196. {    &st_sproc_state, NULL, s_proc_write_process, 0, 0, NULL
  197. };
  198.  
  199. /* Allocate and open a procedure-based write stream. */
  200. int
  201. swrite_proc(ref *sop, stream **psstrm)
  202. {    return s_proc_init(sop, psstrm, s_mode_write, &s_proc_write_template);
  203. }
  204.  
  205. /* Handle an output request. */
  206. #define ss ((stream_proc_state *)st)
  207. private int
  208. s_proc_write_process(stream_state *st, stream_cursor_read *pr,
  209.   stream_cursor_write *ignore_pw, bool last)
  210. {    return ((ss->eof = last && pr->ptr == pr->limit) ? EOFC : CALLC);
  211. }
  212. #undef ss
  213.  
  214. /* Call the procedure when the buffer is full. */
  215. int
  216. s_proc_write_call(stream *s, const ref *fop, const ref *pstate,
  217.   int (*cont)(P1(os_ptr)))
  218. {    int npush;
  219.     stream *ps = s;
  220.     /* Find the stream whose buffer needs emptying. */
  221.     while ( ps->strm != 0 )
  222.       ps = ps->strm;
  223. #define psst ((stream_proc_state *)ps->state)
  224.     if ( psst->eof )
  225.     {    /* This is the final call from closing the stream. */
  226.         /* Don't run the continuation. */
  227.         check_estack(5);
  228.         esp += 5;
  229.         make_op_estack(esp - 4, zpop);    /* pop the file */
  230.         make_op_estack(esp - 3, zpop);    /* pop the string returned */
  231.                         /* by the procedure */
  232.         make_false(esp - 1);
  233.     }
  234.     else
  235.     {    if ( pstate )
  236.         {    check_estack(7);
  237.             ref_assign(esp + 2, pstate);
  238.             npush = 7;
  239.         }
  240.         else
  241.         {    check_estack(6);
  242.             npush = 6;
  243.         }
  244.         make_op_estack(esp + 1, cont);
  245.         esp += npush;
  246.         make_op_estack(esp - 4, s_proc_write_continue);
  247.         esp[-3] = *fop;
  248.         r_clear_attrs(esp - 3, a_executable);
  249.         make_true(esp - 1);
  250.     }
  251.     esp[-2] = psst->proc;
  252. #undef psst
  253.     make_string(esp, a_all, ps->swptr - ps->cbuf + 1,
  254.             ps->cbuf); /* WHAT ABOUT a_local??? */
  255.     return o_push_estack;
  256. }
  257. /* Continue a write operation after returning from a procedure callout. */
  258. /* osp[0] contains the file (pushed on the e-stack by handle_write_status); */
  259. /* osp[-1] contains the new buffer string (pushed by the procedure). */
  260. /* The top of the e-stack contains the real continuation. */
  261. private int
  262. s_proc_write_continue(os_ptr op)
  263. {    os_ptr pbuf = op - 1;
  264.     stream *ps;
  265.     check_file(ps, op);
  266.     check_write_type(*pbuf, t_string);
  267.     while ( (ps->end_status = 0, ps->strm) != 0 )
  268.       ps = ps->strm;
  269.     /* Set srptr so that stream_compact won't get confused. */
  270.     ps->srptr = ps->swptr = (ps->cbuf = pbuf->value.bytes) - 1;
  271.     ps->swlimit = ps->swptr + r_size(pbuf);
  272.     pop(2);
  273.     return 0;
  274. }
  275.  
  276. /* ------ Initialization procedure ------ */
  277.  
  278. op_def zfproc_op_defs[] = {
  279.         /* Internal operators */
  280.     {"2%s_proc_read_continue", s_proc_read_continue},
  281.     {"2%s_proc_write_continue", s_proc_write_continue},
  282.     op_def_end(0)
  283. };
  284.