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

  1. /* Copyright (C) 1993, 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. /* ziodev.c */
  20. /* Standard IODevice implementation */
  21. #include "stdio_.h"
  22. #include "string_.h"
  23. #include "ghost.h"
  24. #include "gp.h"
  25. #include "gpcheck.h"
  26. #include "gsstruct.h"            /* for registering root */
  27. #include "gsparam.h"
  28. #include "errors.h"
  29. #include "oper.h"
  30. #include "stream.h"
  31. #include "ialloc.h"
  32. #include "gxiodev.h"            /* must come after stream.h */
  33.                     /* and before files.h */
  34. #include "files.h"
  35. #include "store.h"
  36.  
  37. /* Complete the definition of the %os% device. */
  38. /* The open_file routine is exported for pipes and for %null. */
  39. int
  40. iodev_os_open_file(gx_io_device *iodev, const char *fname, uint len,
  41.   const char *file_access, stream **ps)
  42. {    return file_open_stream(fname, len, file_access,
  43.                 file_default_buffer_size, ps,
  44.                 iodev->procs.fopen);
  45. }
  46.  
  47. /* Define the special devices. */
  48. #define iodev_special(dname, init, open)\
  49.   { dname, "Special",\
  50.      { init, open, iodev_no_open_file, iodev_no_fopen, iodev_no_fclose,\
  51.        iodev_no_delete_file, iodev_no_rename_file, iodev_no_file_status,\
  52.        iodev_no_enumerate_files, NULL, NULL,\
  53.        iodev_no_get_params, iodev_no_put_params\
  54.      }\
  55.   }
  56.  
  57. #define stdin_buf_size 128
  58. stream *gs_stream_stdin;    /* exported for zfileio.c only */
  59. bool gs_stdin_is_interactive;    /* exported for command line only */
  60. private iodev_proc_init(stdin_init);
  61. private iodev_proc_open_device(stdin_open);
  62. gx_io_device gs_iodev_stdin =
  63.   iodev_special("%stdin%", stdin_init, stdin_open);
  64.  
  65. #define stdout_buf_size 128
  66. stream *gs_stream_stdout;    /* exported for zfileio.c only */
  67. private iodev_proc_init(stdout_init);
  68. private iodev_proc_open_device(stdout_open);
  69. gx_io_device gs_iodev_stdout =
  70.   iodev_special("%stdout%", stdout_init, stdout_open);
  71.  
  72. #define stderr_buf_size 128
  73. stream *gs_stream_stderr;    /* exported for zfileio.c only */
  74. private iodev_proc_init(stderr_init);
  75. private iodev_proc_open_device(stderr_open);
  76. gx_io_device gs_iodev_stderr =
  77.   iodev_special("%stderr%", stderr_init, stderr_open);
  78.  
  79. #define lineedit_buf_size 160
  80. private iodev_proc_open_device(lineedit_open);
  81. gx_io_device gs_iodev_lineedit =
  82.   iodev_special("%lineedit%", iodev_no_init, lineedit_open);
  83.  
  84. private iodev_proc_open_device(statementedit_open);
  85. gx_io_device gs_iodev_statementedit =
  86.   iodev_special("%statementedit%", iodev_no_init, statementedit_open);
  87.  
  88. /* ------ Operators ------ */
  89.  
  90. /* <int> .getiodevice <string> */
  91. int
  92. zgetiodevice(register os_ptr op)
  93. {    gx_io_device *iodev;
  94.     const byte *dname;
  95.     check_type(*op, t_integer);
  96.     if ( op->value.intval != (int)op->value.intval )
  97.         return_error(e_rangecheck);
  98.     iodev = gs_getiodevice((int)(op->value.intval));
  99.     if ( iodev == 0 )        /* index out of range */
  100.         return_error(e_rangecheck);
  101.     dname = (const byte *)iodev->dname;
  102.     make_const_string(op, a_readonly | a_foreign,
  103.               strlen((char *)dname), dname);
  104.     return 0;
  105. }
  106.  
  107. /* ------- %stdin, %stdout, and %stderr ------ */
  108.  
  109. private gs_gc_root_t stdin_root, stdout_root, stderr_root;
  110.  
  111. private int
  112.   s_stdin_read_process(P4(stream_state *, stream_cursor_read *,
  113.     stream_cursor_write *, bool));
  114.  
  115. private int
  116. stdin_init(gx_io_device *iodev, gs_memory_t *mem)
  117. {
  118.     /****** stdin SHOULD NOT LINE-BUFFER ******/
  119.  
  120.     byte *buf = gs_alloc_bytes(mem, stdin_buf_size, "stdin_init");
  121.     stream *s = s_alloc(mem, "stdin_init");
  122.     sread_file(s, gs_stdin, buf, stdin_buf_size);
  123.     /* We want stdin to read only one character at a time, */
  124.     /* but it must have a substantial buffer, in case it is used */
  125.     /* by a stream that requires more than one input byte */
  126.     /* to make progress. */
  127.     s->procs.process = s_stdin_read_process;
  128.     s_init_read_id(s);
  129.     s->procs.close = file_close_finish;
  130.     s->prev = s->next = 0;
  131.     gs_stream_stdin = s;
  132.     gs_stdin_is_interactive = true;
  133.     gs_register_struct_root(imemory, &stdin_root,
  134.                 (void **)&gs_stream_stdin, "gs_stream_stdin");
  135.     return 0;
  136. }
  137.  
  138. /* Read from stdin into the buffer. */
  139. /* If interactive, only read one character. */
  140. private int
  141. s_stdin_read_process(stream_state *st, stream_cursor_read *ignore_pr,
  142.   stream_cursor_write *pw, bool last)
  143. {    FILE *file = ((stream *)st)->file;    /* hack for file streams */
  144.     int wcount = (int)(pw->limit - pw->ptr);
  145.     int count = fread(pw->ptr + 1, 1,
  146.               (gs_stdin_is_interactive ? min(wcount, 1) : wcount),
  147.               file);
  148.     if ( count < 0 )
  149.         count = 0;
  150.     pw->ptr += count;
  151.     (void)gp_check_interrupts();
  152.     return (ferror(file) ? ERRC : feof(file) ? EOFC : count == wcount ? 1 : 0);
  153. }
  154.  
  155. private int
  156. stdin_open(gx_io_device *iodev, const char *access, stream **ps,
  157.   gs_memory_t *mem)
  158. {    if ( !streq1(access, 'r') )
  159.         return_error(e_invalidfileaccess);
  160.     *ps = gs_stream_stdin;
  161.     return 0;
  162. }
  163.  
  164. private int
  165. stdout_init(gx_io_device *iodev, gs_memory_t *mem)
  166. {    byte *buf = gs_alloc_bytes(mem, stdout_buf_size, "stdout_init");
  167.     stream *s = s_alloc(mem, "stdout_init");
  168.     swrite_file(s, gs_stdout, buf, stdout_buf_size);
  169.     s_init_write_id(s);
  170.     s->procs.close = file_close_finish;
  171.     s->prev = s->next = 0;
  172.     gs_stream_stdout = s;
  173.     gs_register_struct_root(imemory, &stdout_root,
  174.                 (void **)&gs_stream_stdout, "gs_stream_stdout");
  175.     return 0;
  176. }
  177.  
  178. private int
  179. stdout_open(gx_io_device *iodev, const char *access, stream **ps,
  180.   gs_memory_t *mem)
  181. {    if ( !streq1(access, 'w') )
  182.         return_error(e_invalidfileaccess);
  183.     *ps = gs_stream_stdout;
  184.     return 0;
  185. }
  186.  
  187. private int
  188. stderr_init(gx_io_device *iodev, gs_memory_t *mem)
  189. {    byte *buf = gs_alloc_bytes(mem, stderr_buf_size, "stderr_init");
  190.     stream *s = s_alloc(mem, "stderr_init");
  191.     swrite_file(s, gs_stderr, buf, stderr_buf_size);
  192.     s_init_write_id(s);
  193.     s->procs.close = file_close_finish;
  194.     s->prev = s->next = 0;
  195.     gs_stream_stderr = s;
  196.     gs_register_struct_root(imemory, &stderr_root,
  197.                 (void **)&gs_stream_stderr, "gs_stream_stderr");
  198.     return 0;
  199. }
  200.  
  201. private int
  202. stderr_open(gx_io_device *iodev, const char *access, stream **ps,
  203.   gs_memory_t *mem)
  204. {    if ( !streq1(access, 'w') )
  205.         return_error(e_invalidfileaccess);
  206.     *ps = gs_stream_stderr;
  207.     return 0;
  208. }
  209.  
  210. /* ------ %lineedit and %statementedit ------ */
  211.  
  212. private int
  213. lineedit_open(gx_io_device *iodev, const char *access, stream **ps,
  214.   gs_memory_t *mem)
  215. {    uint count = 0;
  216.     bool in_eol = false;
  217.     int code;
  218.     stream *s;
  219.     byte *buf;
  220. #define max_line lineedit_buf_size
  221.     byte line[max_line];
  222.     if ( strcmp(access, "r") )
  223.         return_error(e_invalidfileaccess);
  224.     s = file_alloc_stream();
  225.     if ( s == 0 )
  226.         return_error(e_VMerror);
  227.     code = zreadline_from(gs_stream_stdin, line, max_line,
  228.                   &count, &in_eol);
  229. #undef max_line
  230.     if ( code != 0 )
  231.         return_error((code == EOFC ? e_undefinedfilename : e_ioerror));
  232.     buf = gs_alloc_bytes(mem, count, "lineedit_open");
  233.     if ( buf == 0 )
  234.         return_error(e_VMerror);
  235.     memcpy(buf, line, count);
  236.     sread_string(s, buf, count);
  237.     s->save_close = s->procs.close;
  238.     s->procs.close = file_close_disable;
  239.     *ps = s;
  240.     return 0;
  241. }
  242.  
  243. private int
  244. statementedit_open(gx_io_device *iodev, const char *access, stream **ps,
  245.   gs_memory_t *mem)
  246. {    /* NOT IMPLEMENTED PROPERLY YET */
  247.     return lineedit_open(iodev, access, ps, mem);
  248. }
  249.  
  250. /* ------ Initialization procedure ------ */
  251.  
  252. op_def ziodev_op_defs[] = {
  253.     {"1.getiodevice", zgetiodevice},
  254.     op_def_end(0)
  255. };
  256.