home *** CD-ROM | disk | FTP | other *** search
/ Mega Top 1 / os2_top1.zip / os2_top1 / APPS / TEKST / GSPMSRC / SRC / GSOS2.C < prev    next >
C/C++ Source or Header  |  1993-12-25  |  9KB  |  285 lines

  1. #define INCL_DOS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <process.h>
  6. #include <os2.h>
  7. #include "gsos2.h"
  8.  
  9. #define BUFSIZE   2048
  10. #define CMD_PIPE_BUFF 16384L
  11.  
  12. void  WriteStdOut (void *t);
  13. void  ReadWinIn (void *t);
  14. void  WriteCmdPipe (void *t);
  15.  
  16. HPIPE  HPipeStdIn, HPipeStdOut, HPipeWinIn, HPipeCmdPipe;
  17. HEV    hevPipesOpened;         /*  Handle to event semaphore  */
  18.  
  19. main (int argc, char *argv[])
  20. {
  21.     ULONG       cbWritten;
  22.     TID         ThreadId;
  23.     int         i;
  24.     CHAR        buf [BUFSIZE], *p, *q;
  25.     STARTDATA           StartData;      /*  Start session data structure  */
  26.     ULONG               SessID;         /*  Session ID (returned)  */
  27.     PID                 pid;            /*  Process ID (returned)  */
  28.     static UCHAR        PgmTitle [] = "GhostScript 2.6.1";
  29.                                         /*  Program title string  */
  30.     static UCHAR        PgmName [] = "gsos2pm.exe";
  31.                                         /*  Program pathname string  */
  32.     UCHAR               PgmInputs[1024]; /*  Input arguments to the program  */
  33.     UCHAR               ObjBuf[100];    /*  Object buffer  */
  34.     APIRET              rc;             /*  Return code  */
  35.     BOOL                quote;
  36.  
  37.  
  38.     /*  Create named pipes in nonblocking mode so DosConnectNPipe would  */
  39.     /*    return immediately and put the pipes in listening mode.        */
  40.  
  41.     rc = DosCreateNPipe(PIPE_GS_STDIN, &HPipeStdIn,
  42.                         NP_INHERIT | NP_ACCESS_OUTBOUND,
  43.                         NP_NOWAIT | NP_TYPE_BYTE | NP_READMODE_BYTE | 0x01,
  44.                         4096L, 0L, 10000L);
  45.     if (rc != 0)
  46.     {
  47.         printf("DosCreateNPipe (gsstdin) error: return code = %ld.\n", rc);
  48.         DosExit (EXIT_PROCESS, 1L);
  49.     }
  50.  
  51.     rc = DosCreateNPipe(PIPE_GS_STDOUT, &HPipeStdOut,
  52.                         NP_INHERIT | NP_ACCESS_INBOUND,
  53.                         NP_NOWAIT | NP_TYPE_BYTE | NP_READMODE_BYTE | 0x01,
  54.                         0L, 4096L, 10000L);
  55.     if (rc != 0)
  56.     {
  57.         printf("DosCreateNPipe (gsstdout) error: return code = %ld.\n", rc);
  58.         DosExit (EXIT_PROCESS, 1L);
  59.     }
  60.  
  61.     rc = DosCreateNPipe(PIPE_GS_WININ, &HPipeWinIn,
  62.                         NP_INHERIT | NP_ACCESS_INBOUND,
  63.                         NP_NOWAIT | NP_TYPE_BYTE | NP_READMODE_BYTE | 0x01,
  64.                         0L, 4096L, 10000L);
  65.     if (rc != 0)
  66.     {
  67.         printf("DosCreateNPipe (gswinin) error: return code = %ld.\n", rc);
  68.         DosExit (EXIT_PROCESS, 1L);
  69.     }
  70.  
  71.     rc = DosConnectNPipe (HPipeWinIn);
  72.  
  73.     rc = DosConnectNPipe (HPipeStdOut);
  74.  
  75.     rc = DosConnectNPipe (HPipeStdIn);
  76.  
  77.     DosCreateEventSem (SEM_NAMED_PIPES, &hevPipesOpened, 0L, FALSE);
  78.  
  79.     p = PgmInputs;
  80.     for (i = 1; i < argc; i++)
  81.     {
  82.     quote = (strchr(argv[i], ' ') != NULL);
  83.     if (quote)
  84.       *p++ = '"';
  85.     for (q = argv[i]; *q; q++)
  86.     {
  87.       if (*q == '"' || *q == '\\')
  88.         *p++ = '\\';
  89.       *p++ = *q;
  90.     }
  91.     if (quote)
  92.       *p++ = '"';
  93.         *p++ = ' ';
  94.     }
  95.     *p = '\0';
  96.  
  97.     /*  Specify the various session start parameters.  */
  98.  
  99.     StartData.Length = sizeof(STARTDATA);
  100.     StartData.Related = SSF_RELATED_CHILD;
  101.     StartData.FgBg = SSF_FGBG_BACK;
  102.     StartData.TraceOpt = SSF_TRACEOPT_NONE;
  103.     StartData.PgmTitle = PgmTitle;
  104.     StartData.PgmName = PgmName;
  105.     StartData.PgmInputs = PgmInputs;
  106.     StartData.TermQ = 0;
  107.     StartData.Environment = 0;
  108.     StartData.InheritOpt = SSF_INHERTOPT_PARENT;
  109.     StartData.SessionType = SSF_TYPE_PM;
  110.     StartData.IconFile = 0;
  111.     StartData.PgmHandle = 0;
  112.     StartData.PgmControl = SSF_CONTROL_INVISIBLE;
  113.     StartData.InitXPos = 0;
  114.     StartData.InitYPos = 0;
  115.     StartData.InitXSize = 200;
  116.     StartData.InitYSize = 200;
  117.     StartData.Reserved = 0;
  118.     StartData.ObjectBuffer = ObjBuf;
  119.     StartData.ObjectBuffLen = sizeof (ObjBuf);
  120.  
  121.     rc = DosStartSession (&StartData, &SessID, &pid);
  122.  
  123.     if (rc != 0)
  124.     {
  125.         printf ("DosStartSession error: return code = %ld.\n", rc);
  126.         DosExit (EXIT_PROCESS, 1L);
  127.     }
  128.  
  129.     /*  Wait until named pipes have been opened by gsos2pm.exe before */
  130.     /*    setting the named pipes to blocking mode.                   */
  131.     DosWaitEventSem (hevPipesOpened, SEM_INDEFINITE_WAIT);
  132.  
  133.     DosSetNPHState (HPipeWinIn, NP_WAIT | NP_READMODE_BYTE);
  134.     DosSetNPHState (HPipeStdOut, NP_WAIT | NP_READMODE_BYTE);
  135.     DosSetNPHState (HPipeStdIn, NP_WAIT | NP_READMODE_BYTE);
  136.  
  137.     /*  Create a thread that reads input from the drawing window  */
  138.     /*    and writes them to the stdin of GhostScript.            */
  139.     ThreadId = _beginthread (ReadWinIn, NULL, 0x20000, NULL);
  140.     if (ThreadId == -1)
  141.     {
  142.         printf("_beginthread (ReadWinIn) error!\n");
  143.         DosExit (EXIT_PROCESS, 1L);
  144.     }
  145.  
  146.     /*  Create a thread that reads the output from the stdout of  */
  147.     /*    GhostScript and writes them to the console.             */
  148.     ThreadId = _beginthread (WriteStdOut, NULL, 0x20000, NULL);
  149.     if (ThreadId == -1)
  150.     {
  151.         printf("_beginthread (WriteStdOut) error!\n");
  152.         DosExit (EXIT_PROCESS, 1L);
  153.     }
  154.  
  155.     /*  Create a thread that simulates a command pipe for Ghostscript  */
  156.     ThreadId = _beginthread (WriteCmdPipe, NULL, 0x20000, NULL);
  157.     if (ThreadId == -1)
  158.     {
  159.         printf("_beginthread (WriteCmdPipe) error!\n");
  160.         DosExit (EXIT_PROCESS, 1L);
  161.     }
  162.  
  163.     while (fgets (buf, BUFSIZE - 1, stdin) != NULL)
  164.         DosWrite (HPipeStdIn, buf, (ULONG) strlen (buf), &cbWritten);
  165. }
  166.  
  167.  
  168. /*  This thread reads the output from the stdout of GhostScript and  */
  169. /*    writes them to the console.                                    */
  170. void WriteStdOut (void *t)
  171. {
  172.     ULONG ulBytesRead;
  173.     CHAR buf [BUFSIZE];
  174.  
  175.     do
  176.     {
  177.         DosRead (HPipeStdOut, buf, BUFSIZE - 1, &ulBytesRead);
  178.         if (ulBytesRead > 0)
  179.         {
  180.             buf [ulBytesRead] = '\0';
  181.             fputs (buf, stdout);
  182.             fflush (stdout);
  183.         }
  184.     }
  185.     while (ulBytesRead > 0);
  186.  
  187.     /*  GhostScript has exited, and has closed the named pipe HPipeStdOut.  */
  188.     /*    So terminate all the threads in the process using DosExit with    */
  189.     /*    the EXIT_PROCESS parameter.                                       */
  190.  
  191.     DosExit (EXIT_PROCESS, 0L);
  192. }
  193.  
  194.  
  195. /*  This thread reads the input from the drawing window and writes them  */
  196. /*    to the stdin of GhostScript.                                       */
  197. void ReadWinIn (void *t)
  198. {
  199.     ULONG cbWritten, length;
  200.     CHAR buf [BUFSIZE], *pchar;
  201.  
  202.     pchar = buf;
  203.  
  204.     do
  205.     {
  206.         DosRead (HPipeWinIn, pchar, BUFSIZE - 1, &length);
  207.         if (length > 0)
  208.         {
  209.             switch (*pchar)
  210.             {
  211.                 case '\n':
  212.                     pchar += 1;
  213.                     DosWrite (1, "\r\n", 2, &cbWritten);
  214.                     DosWrite (HPipeStdIn, buf, (ULONG) (pchar - buf),
  215.                               &cbWritten);
  216.                     pchar = buf;
  217.                     break;
  218.  
  219.                 case '\b':
  220.                     if (pchar - buf > 0)
  221.                     {
  222.                         DosWrite (1, "\b \b", 3, &cbWritten);
  223.                         pchar -= 1;
  224.                     }
  225.                     break;
  226.  
  227.                 default:
  228.                     DosWrite (1, pchar, 1, &cbWritten);
  229.                     pchar += 1;
  230.                     break;
  231.             }
  232.         }
  233.     }
  234.     while (length > 0);
  235. }
  236.  
  237.  
  238. /*  This thread simulates a command pipe for GhostScript  */
  239. void WriteCmdPipe (void *t)
  240. {
  241.     ULONG ulBytesRead;
  242.     CHAR buf [BUFSIZE];
  243.     APIRET rc;
  244.     FILE *pipe;
  245.  
  246.  
  247.     /*  Create a named pipe in blocking mode  */
  248.     rc = DosCreateNPipe(PIPE_GS_CMDPIPE, &HPipeCmdPipe,
  249.                         NP_INHERIT | NP_ACCESS_INBOUND,
  250.                         NP_WAIT | NP_TYPE_BYTE | NP_READMODE_BYTE | 0x01,
  251.                         0L, CMD_PIPE_BUFF, 10000L);
  252.  
  253.     if (rc != 0)
  254.     {
  255.         printf("DosCreateNPipe (gscmdpip) error: return code = %ld.\n", rc);
  256.         DosExit (EXIT_PROCESS, 1L);
  257.     }
  258.  
  259.     rc = DosConnectNPipe (HPipeCmdPipe);
  260.     DosRead (HPipeCmdPipe, buf, BUFSIZE - 1, &ulBytesRead);
  261.     buf [ulBytesRead] = '\0';
  262.  
  263.     if ((pipe = popen (buf, "wb")) == NULL)
  264.     {
  265.         printf ("Error: unable to open command pipe!\n");
  266.         DosExit (EXIT_PROCESS, 1L);
  267.     }
  268.  
  269.     DosPostEventSem (hevPipesOpened);
  270.     /*  Inform the Ghostscript thread in gspm.exe that we have  */
  271.     /*    established a pipe to the program requested.            */
  272.  
  273.     do
  274.     {
  275.         DosRead (HPipeCmdPipe, buf, BUFSIZE - 1, &ulBytesRead);
  276.  
  277.         if (ulBytesRead > 0)
  278.             fwrite (buf, (size_t) ulBytesRead, 1, pipe);
  279.  
  280.     }
  281.     while (ulBytesRead > 0);
  282.  
  283.     pclose (pipe);
  284. }
  285.