home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gp_mshdl.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  3.9 KB  |  123 lines

  1. /* Copyright (C) 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gp_mshdl.c,v 1.2 2000/09/19 19:00:24 lpd Exp $ */
  20. /* %handle% IODevice */
  21. #include "errno_.h"
  22. #include "stdio_.h"
  23. #include "string_.h"
  24. #include "ctype_.h"
  25. #include <io.h>
  26. #include "gserror.h"
  27. #include "gstypes.h"
  28. #include "gsmemory.h"        /* for gxiodev.h */
  29. #include "gxiodev.h"
  30.  
  31. /* The MS-Windows handle IODevice */
  32.  
  33. /* This allows an MS-Windows file handle to be passed in to
  34.  * Ghostscript as %handle%NNNNNNNN where NNNNNNNN is the hexadecimal
  35.  * value of the handle.  
  36.  * The typical use is for another program to create a pipe, 
  37.  * pass the write end into Ghostscript using 
  38.  *  -sOutputFile="%handle%NNNNNNNN"
  39.  * so that Ghostscript printer output can be captured by the
  40.  * other program.  The handle would be created with CreatePipe().
  41.  * If Ghostscript is not a DLL, the pipe will have to be inheritable 
  42.  * by the Ghostscript process.
  43.  */
  44.  
  45. private iodev_proc_fopen(mswin_handle_fopen);
  46. private iodev_proc_fclose(mswin_handle_fclose);
  47. const gx_io_device gs_iodev_handle = {
  48.     "%handle%", "FileSystem",
  49.     {iodev_no_init, iodev_no_open_device,
  50.      NULL /*iodev_os_open_file */ , mswin_handle_fopen, mswin_handle_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. /* The file device procedures */
  58. #ifndef INVALID_HANDLE_VALUE
  59. #define INVALID_HANDLE_VALUE (-1)
  60. #endif
  61.  
  62. /* Allow printer filename specified by -sOutputFile="..."
  63.  * to contain a hexadecimal encoded OS file handle in the
  64.  * form -sOutputFile="\\handle\\000001c5"
  65.  *
  66.  * Returns:
  67.  *   The OS file handle on success.
  68.  *   INVALID_HANDLE_VALUE on failure.
  69.  *
  70.  * This allows the caller to create an OS pipe and give us a 
  71.  * handle to the write end of the pipe.
  72.  * If we are called as an EXE, the OS file handle must be 
  73.  * inherited by Ghostscript.
  74.  * Pipes aren't supported under Win32s.
  75.  */
  76. private long 
  77. get_os_handle(const char *name)
  78. {
  79.     ulong hfile;    /* This must be as long as the longest handle. */
  80.             /* This is correct for Win32, maybe wrong for Win64. */
  81.     int i, ch;
  82.  
  83.     for (i = 0; (ch = name[i]) != 0; ++i)
  84.     if (!isxdigit(ch))
  85.         return (long)INVALID_HANDLE_VALUE;
  86.     if (sscanf(name, "%lx", &hfile) != 1)
  87.     return (long)INVALID_HANDLE_VALUE;
  88.     return (long)hfile; 
  89. }
  90.  
  91. private int
  92. mswin_handle_fopen(gx_io_device * iodev, const char *fname, const char *access,
  93.        FILE ** pfile, char *rfname, uint rnamelen)
  94. {
  95.     int fd;
  96.     long hfile;    /* Correct for Win32, may be wrong for Win64 */
  97.     errno = 0;
  98.  
  99.     if ((hfile = get_os_handle(fname)) == (long)INVALID_HANDLE_VALUE)
  100.     return_error(gs_fopen_errno_to_code(EBADF));
  101.  
  102.     /* associate a C file handle with an OS file handle */
  103.     fd = _open_osfhandle((long)hfile, 0);
  104.     if (fd == -1)
  105.     return_error(gs_fopen_errno_to_code(EBADF));
  106.  
  107.     /* associate a C file stream with C file handle */
  108.     *pfile = fdopen(fd, (char *)access);
  109.     if (*pfile == NULL)
  110.     return_error(gs_fopen_errno_to_code(errno));
  111.  
  112.     if (rfname != NULL)
  113.     strcpy(rfname, fname);
  114.     return 0;
  115. }
  116.  
  117. private int
  118. mswin_handle_fclose(gx_io_device * iodev, FILE * file)
  119. {
  120.     fclose(file);
  121.     return 0;
  122. }
  123.