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

  1. /* Copyright (C) 1992, 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. /* gp_mswin.c */
  20. /*
  21.  * Microsoft Windows 3.n platform support for Ghostscript.
  22.  * Original version by Russell Lang and Maurice Castro with help from
  23.  * Programming Windows, 2nd Ed., Charles Petzold, Microsoft Press;
  24.  * initially created from gp_dosfb.c and gp_itbc.c 5th June 1992.
  25.  */
  26.  
  27. #include "stdio_.h"
  28. #include "string_.h"
  29. #include "memory_.h"
  30. #include <stdlib.h>
  31. #include <stdarg.h>
  32. #include "ctype_.h"
  33. #include "dos_.h"
  34. #include <io.h>
  35. #include "malloc_.h"
  36. #include <fcntl.h>
  37. #include <signal.h>
  38. #include "gx.h"
  39. #include "gp.h"
  40. #include "gpcheck.h"
  41. #include "gserrors.h"
  42. #include "gsexit.h"
  43. #include "gxdevice.h"
  44.  
  45. #include "windows_.h"
  46. #if WINVER >= 0x030a
  47. #include <shellapi.h>
  48. #endif
  49. #include "gp_mswin.h"
  50. #include "gp_mswtx.h"
  51. #ifdef __DLL__
  52. #include "gsdll.h"
  53. #endif
  54.  
  55. /* for imitation pipes */
  56. #include "stream.h"
  57. #include "gxiodev.h"            /* must come after stream.h */
  58. extern stream *gs_stream_stdin;        /* from ziodev.c */
  59. extern stream *gs_stream_stdout;    /* from ziodev.c */
  60. extern stream *gs_stream_stderr;    /* from ziodev.c */
  61.  
  62. /* Library routines not declared in a standard header */
  63. extern char *getenv(P1(const char *));
  64.  
  65. /* Imported from gp_msdos.c */
  66. int gp_file_is_console(P1(FILE *));
  67.  
  68. /* ------ from gnuplot winmain.c plus new stuff ------ */
  69.  
  70. /* limits */
  71. #define MAXSTR 255
  72.  
  73. /* public handles */
  74. HWND hwndtext;
  75. HINSTANCE phInstance;
  76.  
  77. const LPSTR szAppName = "Ghostscript";
  78. const LPSTR szImgName = "Ghostscript Image";
  79. BOOL is_win31 = FALSE;
  80. char FAR win_prntmp[MAXSTR];    /* filename of PRN temporary file */
  81. int win_init = 0;        /* flag to know if gp_exit has been called */
  82. int win_exit_status;
  83.  
  84. /* gsview.exe */
  85. BOOL gsview = FALSE;
  86. HWND gsview_hwnd = NULL;
  87. BOOL gsview_next = FALSE;
  88. LPSTR gsview_option = "-sGSVIEW=";
  89.  
  90. /* redirected stdio */
  91. TW textwin;    /* text window structure */
  92.  
  93. /* imitation pipes */
  94. HGLOBAL pipe_hglobal = NULL;
  95. LPBYTE pipe_lpbyte = NULL;
  96. UINT pipe_count = 0;
  97.  
  98. BOOL CALLBACK _export AbortProc(HDC, int);
  99.  
  100. #ifdef __DLL__
  101. int WINAPI _export
  102. LibMain(HINSTANCE hInstance, WORD wDataSeg, WORD wHeapSize, LPSTR lpszCmdLine)
  103. {
  104.     phInstance = hInstance;
  105.     return 1;
  106. }
  107.  
  108. int WINAPI _export
  109. WEP(int nParam)
  110. {
  111.     return 1;
  112. }
  113. #else
  114. int main(int argc, char *argv[]);
  115.  
  116. /* our exit handler */
  117. /* also called from Text Window WM_CLOSE */
  118. void win_exit(void)
  119. {
  120. #if WINVER >= 0x030a
  121.     /* disable Drag Drop */
  122.     if (is_win31)
  123.         DragAcceptFiles(hwndtext, FALSE);
  124. #endif
  125.     /* if we didn't exit through gs_exit() then do so now */
  126.     if (win_init)
  127.         gs_exit(0);
  128.  
  129.     fcloseall();
  130.     if (win_exit_status) {
  131.         /* display message box so error messages in hwndtext can be read */
  132.         char buf[20];
  133.         if (IsIconic(textwin.hWndText))
  134.             ShowWindow(textwin.hWndText, SW_SHOWNORMAL);
  135.         BringWindowToTop(textwin.hWndText);  /* make hwndtext visible */
  136.         sprintf(buf, "Exit code %d\nSee text window for details",win_exit_status);
  137.         MessageBox((HWND)NULL, buf, szAppName, MB_OK | MB_ICONSTOP);
  138.     }
  139.     
  140.     /* tell gsview that we are closing */
  141.     if (gsview)
  142.         SendMessage(gsview_hwnd, WM_GSVIEW, GSWIN_CLOSE, (LPARAM)NULL);
  143.  
  144.     TextClose(&textwin);
  145.     exit(win_exit_status);
  146. }
  147.  
  148. int PASCAL 
  149. WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int cmdShow)
  150. {
  151.     int i;
  152. #if defined(_MSC_VER)    /* MSC doesn't give us _argc and _argv[] so ...   */
  153. #define MAXCMDTOKENS 128
  154.     int    _argc=0;
  155.     LPSTR    _argv[MAXCMDTOKENS];
  156.     _argv[_argc] = "gswin.exe";
  157.     _argv[++_argc] = _fstrtok( lpszCmdLine, " ");
  158.     while (_argv[_argc] != NULL)
  159.         _argv[++_argc] = _fstrtok( NULL, " ");
  160. #endif
  161.     is_win31 = FALSE;
  162.     {
  163.     WORD version = LOWORD(GetVersion());
  164.     if (((LOBYTE(version)<<8) | HIBYTE(version)) >= 0x30a)
  165.         is_win31 = TRUE;
  166.     }
  167.  
  168.     if (hPrevInstance) {
  169.         MessageBox((HWND)NULL,"Can't run twice", szAppName, MB_ICONHAND | MB_OK);
  170.         return FALSE;
  171.     }
  172.  
  173.     /* copy the hInstance into a variable so it can be used */
  174.     phInstance = hInstance;
  175.  
  176.     /* start up the text window */
  177.     textwin.hInstance = hInstance;
  178.     textwin.hPrevInstance = hPrevInstance;
  179.     textwin.nCmdShow = cmdShow;
  180.     textwin.Title = szAppName;
  181.     textwin.hIcon = LoadIcon(hInstance, (LPSTR)MAKEINTRESOURCE(TEXT_ICON));
  182.     textwin.DragPre = "(";
  183.     textwin.DragPost = ") run\r";
  184.     textwin.ScreenSize.x = 80;
  185.     textwin.ScreenSize.y = 80;
  186.     textwin.KeyBufSize = 2048;
  187.     textwin.CursorFlag = 1;    /* scroll to cursor after \n & \r */
  188.     textwin.shutdown = win_exit;
  189.  
  190.     if (TextInit(&textwin))
  191.         exit(1);
  192.     hwndtext = textwin.hWndText;
  193.  
  194.     (void) atexit((atexit_t)win_exit); /* setup exit handler */
  195.  
  196.     /* check if we are to use gsview.exe */
  197.     for (i=0; i<_argc; i++) {
  198.         if (!strncmp(_argv[i], gsview_option, strlen(gsview_option))) {
  199.         gsview_hwnd = (HWND)atoi(_argv[i]+strlen(gsview_option));
  200.         if (gsview_hwnd != (HWND)NULL) {
  201.             if (!IsWindow(gsview_hwnd)) {
  202.                 char buf[80];
  203.                 sprintf(buf,"GSVIEW window handle %u is invalid",(int)gsview_hwnd);
  204.                 MessageBox(hwndtext, buf, szAppName, MB_OK);
  205.                 return 0;
  206.             }
  207.             gsview = TRUE;
  208.             /* give gsview the handle to our text window */
  209.             SendMessage(gsview_hwnd, WM_GSVIEW, HWND_TEXT, (LPARAM)hwndtext);
  210.         }
  211.         }
  212.     }
  213.  
  214.     main(_argc, _argv);
  215.  
  216.     /* never reached */
  217.     win_exit(); 
  218.     return 0;
  219. }
  220.  
  221. #endif /* !__DLL__ */
  222.  
  223. BOOL CALLBACK _export
  224. AbortProc(HDC hdcPrn, int code)
  225. {
  226.     (void)gp_check_interrupts();
  227.     return(TRUE);
  228. }
  229.   
  230. /* ------ Process message loop ------ */
  231. /*
  232.  * Check messages and interrupts; return true if interrupted.
  233.  * This is called frequently - it must be quick!
  234.  */
  235. int
  236. gp_check_interrupts(void)
  237. {
  238.     TextMessage();
  239.     return 0;
  240. }
  241.  
  242. /* ====== Generic platform procedures ====== */
  243.  
  244. /* ------ Initialization/termination (from gp_itbc.c) ------ */
  245.  
  246. /* Do platform-dependent initialization. */
  247. void
  248. gp_init(void)
  249. {
  250.     win_init = 1;
  251. }
  252.  
  253. /* Do platform-dependent cleanup. */
  254. void
  255. gp_exit(int exit_status, int code)
  256. {
  257.     win_init = 0;
  258.     win_exit_status = exit_status;
  259. }
  260.  
  261. /* ------ Printer accessing ------ */
  262.   
  263. /* Forward references */
  264. private int gp_printfile(P2(char *, const char *));
  265.  
  266. /* Open a connection to a printer.  A null file name means use the */
  267. /* standard printer connected to the machine, if any. */
  268. /* Return NULL if the connection could not be opened. */
  269. FILE *
  270. gp_open_printer(char *fname, int binary_mode)
  271. {
  272. char buf[128];
  273.     /* if fname appears in win.ini ports section, then treat as printer */
  274.     GetProfileString("ports", fname, "XYZ", buf, sizeof(buf));
  275.     if ( strlen(fname) == 0 || !strcmp(fname, "PRN") || !strcmp(buf,"XYZ") )
  276.     {    FILE *pfile;
  277.         pfile = gp_open_scratch_file(gp_scratch_file_name_prefix, 
  278.             win_prntmp, "wb");
  279.         return pfile;
  280.     }
  281.     else
  282.         return fopen(fname, (binary_mode ? "wb" : "w"));
  283. }
  284.  
  285. /* Close the connection to the printer. */
  286. void
  287. gp_close_printer(FILE *pfile, const char *fname)
  288. {
  289. char buf[128];
  290.     fclose(pfile);
  291.     GetProfileString("ports", fname, "XYZ", buf, sizeof(buf));
  292.     if (strlen(fname) && strcmp(fname,"PRN") && strcmp(buf,"XYZ"))
  293.         return;        /* a file, not a printer */
  294.  
  295.     gp_printfile(win_prntmp, fname);
  296.     unlink(win_prntmp);
  297. }
  298.  
  299. #ifndef __WIN32__
  300.  
  301. /* Windows does not provide API's in the SDK for writing directly to a */
  302. /* printer.  Instead you are supposed to use the Windows printer drivers. */
  303. /* Ghostscript has its own printer drivers, so we need to use some API's */
  304. /* that are documented only in the Device Driver Adaptation Guide */
  305. /* that comes with the DDK.  Prototypes taken from DDK <print.h> */
  306. DECLARE_HANDLE(HPJOB);
  307.  
  308. HPJOB    WINAPI OpenJob(LPSTR, LPSTR, HPJOB);
  309. int    WINAPI StartSpoolPage(HPJOB);
  310. int    WINAPI EndSpoolPage(HPJOB);
  311. int    WINAPI WriteSpool(HPJOB, LPSTR, int);
  312. int    WINAPI CloseJob(HPJOB);
  313. int    WINAPI DeleteJob(HPJOB, int);
  314. int    WINAPI WriteDialog(HPJOB, LPSTR, int);
  315. int    WINAPI DeleteSpoolPage(HPJOB);
  316.  
  317. #endif        /* WIN32 */
  318.  
  319. HWND hDlgModeless;
  320.  
  321. /* Modeless dialog box - Cancel printing */
  322. BOOL CALLBACK _export
  323. CancelDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  324. {
  325.     switch(message) {
  326.     case WM_INITDIALOG:
  327.         SetWindowText(hDlg, szAppName);
  328.         return TRUE;
  329.     case WM_COMMAND:
  330.         switch(LOWORD(wParam)) {
  331.         case IDCANCEL:
  332.             DestroyWindow(hDlg);
  333.             hDlgModeless = 0;
  334.             EndDialog(hDlg, 0);
  335.             return TRUE;
  336.         }
  337.     }
  338.     return FALSE;
  339. }
  340.  
  341. /* Dialog box to select printer port */
  342. BOOL CALLBACK _export
  343. SpoolDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  344. {
  345. LPSTR entry;
  346.     switch(message) {
  347.     case WM_INITDIALOG:
  348.         entry = (LPSTR)lParam;
  349.         while (*entry) {
  350.         SendDlgItemMessage(hDlg, SPOOL_PORT, LB_ADDSTRING, 0, (LPARAM)entry);
  351.         entry += lstrlen(entry)+1;
  352.         }
  353.         SendDlgItemMessage(hDlg, SPOOL_PORT, LB_SETCURSEL, 0, (LPARAM)0);
  354.         return TRUE;
  355.     case WM_COMMAND:
  356.         switch(LOWORD(wParam)) {
  357.         case SPOOL_PORT:
  358. #ifdef WIN32
  359.             if (HIWORD(wParam)
  360. #else
  361.             if (HIWORD(lParam)
  362. #endif
  363.                            == LBN_DBLCLK)
  364.             PostMessage(hDlg, WM_COMMAND, IDOK, 0L);
  365.             return FALSE;
  366.         case IDOK:
  367.             EndDialog(hDlg, 1+(int)SendDlgItemMessage(hDlg, SPOOL_PORT, LB_GETCURSEL, 0, 0L));
  368.             return TRUE;
  369.         case IDCANCEL:
  370.             EndDialog(hDlg, 0);
  371.             return TRUE;
  372.         }
  373.     }
  374.     return FALSE;
  375. }
  376.  
  377.  
  378. #ifdef __WIN32__        /* ******** WIN32 ******** */
  379.  
  380. /* Win32s can't get access to OpenJob etc., so we try to sneak the */
  381. /* data through the Windows printer driver unchanged */
  382. BOOL CALLBACK _export
  383. PrintAbortProc(HDC hdcPrn, int code)
  384. {
  385.     MSG msg;
  386.     while (hDlgModeless && PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
  387.     if (hDlgModeless || !IsDialogMessage(hDlgModeless,&msg)) {
  388.             TranslateMessage(&msg);
  389.             DispatchMessage(&msg);
  390.     }
  391.     }
  392.     return(hDlgModeless!=0);
  393. }
  394.  
  395. /* Print File */
  396. private int
  397. gp_printfile(char *filename, const char *pmport)
  398. {
  399. HDC printer;
  400. PRINTDLG pd;
  401. DOCINFO di;
  402. char *buf;
  403. WORD *bufcount;
  404. int count;
  405. FILE *f;
  406. long lsize;
  407. long ldone;
  408. char *fmt = "%d%% done";
  409. char pcdone[10];
  410. int error = TRUE;
  411.     memset(&pd, 0, sizeof(PRINTDLG));
  412.     pd.lStructSize = sizeof(PRINTDLG);
  413.     pd.hwndOwner = hwndtext;
  414.     pd.Flags = PD_PRINTSETUP | PD_RETURNDC;
  415.  
  416.     if ((f = fopen(filename, "rb")) == (FILE *)NULL)
  417.         return FALSE;
  418.     fseek(f, 0L, SEEK_END);
  419.     lsize = ftell(f);
  420.     if (lsize <= 0)
  421.         lsize = 1;
  422.     fseek(f, 0L, SEEK_SET);
  423.     ldone = 0;
  424.  
  425.     if (PrintDlg(&pd)) {
  426.     printer = pd.hDC;
  427.     if (printer != (HDC)NULL) {
  428.         if ( (buf = malloc(4096+2)) != (char *)NULL ) {
  429.             bufcount = (int *)buf;
  430.         EnableWindow(hwndtext,FALSE);
  431.         hDlgModeless = CreateDialogParam(phInstance,"CancelDlgBox",hwndtext,CancelDlgProc,(LPARAM)szAppName);
  432.         SetAbortProc(printer, PrintAbortProc);
  433.         di.cbSize = sizeof(DOCINFO);
  434.         di.lpszDocName = szAppName;
  435.         di.lpszOutput = NULL;
  436.         if (StartDoc(printer, &di) > 0) {
  437.             while ( hDlgModeless &&  ((count = fread(buf+2, 1, 4096, f)) != 0) ) {
  438.             *bufcount = count;
  439.             Escape(printer, PASSTHROUGH, count+2, (LPSTR)buf, NULL);
  440.                 ldone += count;
  441.                 sprintf(pcdone, fmt, (int)(ldone * 100 / lsize));
  442.                 SetWindowText(GetDlgItem(hDlgModeless, CANCEL_PCDONE), pcdone);
  443.             }
  444.             if (hDlgModeless)
  445.             EndDoc(printer);
  446.             else
  447.             AbortDoc(printer);
  448.         }
  449.         if (hDlgModeless) {
  450.             DestroyWindow(hDlgModeless);
  451.             error = FALSE;
  452.             hDlgModeless = (HWND)0;
  453.         }
  454.             EnableWindow(hwndtext,TRUE);
  455.             free(buf);
  456.         }
  457.       }
  458.       DeleteDC(printer);
  459.     }
  460.     fclose(f);
  461.     return !error;
  462. }
  463.  
  464. #else                /* ******** !WIN32 ******** */
  465.  
  466. /* Print File to port */
  467. private int
  468. gp_printfile(char *filename, const char *pmport)
  469. {
  470. #define PRINT_BUF_SIZE 16384u
  471. char *buffer;
  472. char *portname;
  473. int i, port;
  474. FILE *f;
  475. DLGPROC lpfnSpoolProc;
  476. WORD count;
  477. DLGPROC lpfnCancelProc;
  478. int error = FALSE;
  479. long lsize;
  480. long ldone;
  481. char pcdone[20];
  482. MSG msg;
  483. HPJOB hJob;
  484.  
  485.     /* get list of ports */
  486.     if ((buffer = malloc(PRINT_BUF_SIZE)) == (char *)NULL)
  487.         return FALSE;
  488.  
  489.     if ( (strlen(pmport)==0) || (strcmp(pmport, "PRN")==0) ) {
  490.         GetProfileString("ports", NULL, "", buffer, PRINT_BUF_SIZE);
  491.         /* select a port */
  492.         lpfnSpoolProc = (DLGPROC)MakeProcInstance((FARPROC)SpoolDlgProc, phInstance);
  493.         port = DialogBoxParam(phInstance, "SpoolDlgBox", hwndtext, lpfnSpoolProc, (LPARAM)buffer);
  494.         FreeProcInstance((FARPROC)lpfnSpoolProc);
  495.         if (!port) {
  496.             free(buffer);
  497.             return FALSE;
  498.         }
  499.         portname = buffer;
  500.         for (i=1; i<port && strlen(portname)!=0; i++)
  501.             portname += lstrlen(portname)+1;
  502.     }
  503.     else
  504.         portname = (char *)pmport;    /* Print Manager port name already supplied */
  505.     
  506.     if ((f = fopen(filename, "rb")) == (FILE *)NULL) {
  507.         free(buffer);
  508.         return FALSE;
  509.     }
  510.     fseek(f, 0L, SEEK_END);
  511.     lsize = ftell(f);
  512.     if (lsize <= 0)
  513.         lsize = 1;
  514.     fseek(f, 0L, SEEK_SET);
  515.  
  516.     hJob = OpenJob(portname, filename, (HDC)NULL);
  517.     switch ((int)hJob) {
  518.         case SP_APPABORT:
  519.         case SP_ERROR:
  520.         case SP_OUTOFDISK:
  521.         case SP_OUTOFMEMORY:
  522.         case SP_USERABORT:
  523.         fclose(f);
  524.         free(buffer);
  525.         return FALSE;
  526.     }
  527.     if (StartSpoolPage(hJob) < 0)
  528.         error = TRUE;
  529.  
  530.     lpfnCancelProc = (DLGPROC)MakeProcInstance((FARPROC)CancelDlgProc, phInstance);
  531.     hDlgModeless = CreateDialog(phInstance, "CancelDlgBox", hwndtext, lpfnCancelProc);
  532.     ldone = 0;
  533.  
  534.     while (!error && hDlgModeless 
  535.       && (count = fread(buffer, 1, PRINT_BUF_SIZE, f)) != 0 ) {
  536.         if (WriteSpool(hJob, buffer, count) < 0)
  537.         error = TRUE;
  538.         ldone += count;
  539.         sprintf(pcdone, "%d %%done", (int)(ldone * 100 / lsize));
  540.         SetWindowText(GetDlgItem(hDlgModeless, CANCEL_PCDONE), pcdone);
  541.         while (PeekMessage(&msg, hDlgModeless, 0, 0, PM_REMOVE)) {
  542.         if ((hDlgModeless == 0) || !IsDialogMessage(hDlgModeless, &msg)) {
  543.             TranslateMessage(&msg);
  544.             DispatchMessage(&msg);
  545.           }
  546.           }
  547.       }
  548.     free(buffer);
  549.     fclose(f);
  550.  
  551.     if (!hDlgModeless)
  552.         error=TRUE;
  553.     DestroyWindow(hDlgModeless);
  554.     hDlgModeless = 0;
  555.     FreeProcInstance((FARPROC)lpfnCancelProc);
  556.     EndSpoolPage(hJob);
  557.     if (error)
  558.         DeleteJob(hJob, 0);
  559.     else
  560.         CloseJob(hJob);
  561.     return !error;
  562. }
  563.  
  564. #endif                /* ******** (!)WIN32 ******** */
  565.  
  566. /* ------ File names ------ */
  567.  
  568. /* Create and open a scratch file with a given name prefix. */
  569. /* Write the actual file name at fname. */
  570. FILE *
  571. gp_open_scratch_file(const char *prefix, char *fname, const char *mode)
  572. {    char *temp;
  573.     if ( (temp = getenv("TEMP")) == NULL )
  574.         *fname = 0;
  575.     else
  576.     {    strcpy(fname, temp);
  577.         /* Prevent X's in path from being converted by mktemp. */
  578.         for ( temp = fname; *temp; temp++ )
  579.             *temp = tolower(*temp);
  580.         if ( strlen(fname) && (fname[strlen(fname)-1] != '\\') )
  581.             strcat(fname, "\\");
  582.     }
  583.     strcat(fname, prefix);
  584.     strcat(fname, "XXXXXX");
  585.     mktemp(fname);
  586.     return fopen(fname, mode);
  587. }
  588.  
  589. /* ====== Substitute for stdio ====== */
  590.  
  591. /* Forward references */
  592. private void win_std_init(void);
  593. private void win_pipe_init(void);
  594. private stream_proc_process(win_std_read_process);
  595. private stream_proc_process(win_std_write_process);
  596.  
  597. /* Use a pseudo IODevice to get win_stdio_init called at the right time. */
  598. /* This is bad architecture; we'll fix it later. */
  599. private iodev_proc_init(win_stdio_init);
  600. gx_io_device gs_iodev_wstdio = {
  601.     "wstdio", "Special",
  602.     { win_stdio_init, iodev_no_open_device,
  603.       iodev_no_open_file, iodev_no_fopen, iodev_no_fclose,
  604.       iodev_no_delete_file, iodev_no_rename_file,
  605.       iodev_no_file_status, iodev_no_enumerate_files
  606.     }
  607. };
  608.  
  609. /* Do one-time initialization */
  610. private int
  611. win_stdio_init(gx_io_device *iodev, gs_memory_t *mem)
  612. {
  613.     win_std_init();        /* redefine stdin/out/err to our window routines */
  614. #ifndef __DLL__
  615.     if (gsview)
  616.         win_pipe_init();    /* redefine stdin to be a pipe */
  617. #endif
  618.     return 0;
  619. }
  620.  
  621. /* reinitialize stdin/out/err to use our windows */
  622. /* assume stream has already been initialized for the real stdin */
  623. private void
  624. win_std_init(void)
  625. {
  626.     if ( gp_file_is_console(gs_stream_stdin->file) )
  627.     {    /* Allocate a real buffer for stdin. */
  628.         /* The size must not exceed the size of the */
  629.         /* lineedit buffer.  (This is a hack.) */
  630. #define win_stdin_buf_size 160
  631.         static const stream_procs pin =
  632.         {    s_std_noavailable, s_std_noseek, s_std_read_reset,
  633.             s_std_read_flush, s_std_close,
  634.             win_std_read_process
  635.         };
  636.         byte *buf = (byte *)gs_malloc(win_stdin_buf_size, 1,
  637.                           "win_stdin_init");
  638.         s_std_init(gs_stream_stdin, buf, win_stdin_buf_size,
  639.                &pin, s_mode_read);
  640.         gs_stream_stdin->file = NULL;
  641.     }
  642.  
  643.     {    static const stream_procs pout =
  644.         {    s_std_noavailable, s_std_noseek, s_std_write_reset,
  645.             s_std_write_flush, s_std_close,
  646.             win_std_write_process
  647.         };
  648.         if ( gp_file_is_console(gs_stream_stdout->file) )
  649.         {    gs_stream_stdout->procs = pout;
  650.             gs_stream_stdout->file = NULL;
  651.         }
  652.         if ( gp_file_is_console(gs_stream_stderr->file) )
  653.         {    gs_stream_stderr->procs = pout;
  654.             gs_stream_stderr->file = NULL;
  655.         }
  656.     }
  657.  
  658. }
  659.  
  660. #ifdef __DLL__
  661. private int
  662. win_std_read_process(stream_state *st, stream_cursor_read *ignore_pr,
  663.   stream_cursor_write *pw, bool last)
  664. {
  665.     int count = pw->limit - pw->ptr;
  666.  
  667.     if ( count == 0 )         /* empty buffer */
  668.         return 1;
  669.  
  670. /* callback to get more input */
  671.     count = (*pgsdll_callback)(GSDLL_STDIN, pw->ptr+1, count);
  672.     if (count == 0) {
  673.         /* EOF */
  674.         /* what should we do? */
  675.         return EOFC;
  676.     }
  677.  
  678.     pw->ptr += count;
  679.     return 1;
  680. }
  681. #else /* !__DLL__ */
  682. /* We should really use a private buffer for line reading, */
  683. /* because we can't predict the size of the supplied input area.... */
  684. private int
  685. win_std_read_process(stream_state *st, stream_cursor_read *ignore_pr,
  686.   stream_cursor_write *pw, bool last)
  687. {    byte *buf = pw->ptr;
  688.     /* Implement line editing here. */
  689. #define ERASELINE 21        /* ^U */
  690. #define ERASECHAR1 8        /* ^H */
  691. #define ERASECHAR2 127        /* DEL */
  692.     byte *dest = buf;
  693.     byte *limit = pw->limit - 1;    /* always leave room for \n */
  694.     uint ch;
  695.     if ( dest > limit )        /* empty buffer */
  696.         return 1;
  697.     while ( (ch = TextGetCh(&textwin)) != '\n' )
  698.     {    switch ( ch )
  699.         {
  700.         default:
  701.             if ( dest == limit )
  702.                 MessageBeep(-1);
  703.             else
  704.             {    *++dest = ch;
  705.                 TextPutCh(&textwin, ch);
  706.             }
  707.             break;
  708.         case ERASELINE:
  709.             while ( dest > buf )
  710.             {    TextPutCh(&textwin, '\b');
  711.                 TextPutCh(&textwin, ' ');
  712.                 TextPutCh(&textwin, '\b');
  713.                 dest--;
  714.             }
  715.             break;
  716.         case ERASECHAR1:
  717.         case ERASECHAR2:
  718.             if ( dest > buf )
  719.             {    TextPutCh(&textwin, '\b');
  720.                 TextPutCh(&textwin, ' ');
  721.                 TextPutCh(&textwin, '\b');
  722.                 dest--;
  723.             }
  724.             break;
  725.         }
  726.     }
  727.     *++dest = ch;
  728.     TextPutCh(&textwin, ch);
  729.     pw->ptr = dest;
  730.     return 1;
  731. }
  732. #endif /* !__DLL__ */
  733.  
  734.  
  735. private int
  736. win_std_write_process(stream_state *st, stream_cursor_read *pr,
  737.   stream_cursor_write *ignore_pw, bool last)
  738. {    uint count = pr->limit - pr->ptr;
  739. #ifdef __DLL__
  740.     (*pgsdll_callback)(GSDLL_STDOUT, (char *)(pr->ptr +1), count);
  741.     pr->ptr = pr->limit;
  742. #else
  743.     /* The prototype for TextWriteBuf is missing a const.... */
  744.     TextWriteBuf(&textwin, (char *)(pr->ptr + 1), count);
  745.     pr->ptr = pr->limit;
  746.     (void)gp_check_interrupts();
  747. #endif
  748.     return 0;
  749. }
  750.  
  751. /* This is used instead of the stdio version. */
  752. /* The declaration must be identical to that in <stdio.h>. */
  753. int _Cdecl _FARFUNC
  754. fprintf(FILE _FAR *file, const char *fmt, ...)
  755. {
  756. int count;
  757. va_list args;
  758.     va_start(args,fmt);
  759.     if ( gp_file_is_console(file) ) {
  760.         char buf[1024];
  761.         count = vsprintf(buf,fmt,args);
  762. #ifdef __DLL__
  763.         (*pgsdll_callback)(GSDLL_STDOUT, buf, count);
  764. #else
  765. /*        MessageBox((HWND)NULL, buf, szAppName, MB_OK | MB_ICONSTOP); */
  766.         TextWriteBuf(&textwin, buf, count);
  767. #endif
  768.     }
  769.     else
  770.         count = vfprintf(file, fmt, args);
  771.     va_end(args);
  772.     return count;
  773. }
  774.  
  775. /* ------ Imitation pipes (only used with gsview) ------ */
  776.  
  777. /* Forward references */
  778. private stream_proc_process(win_pipe_read_process);
  779. private void win_pipe_request(void);
  780. private int win_pipe_read(char *, unsigned int);
  781.  
  782. /* reinitialize stdin stream to read from imitation pipe */
  783. /* assume stream has already been initialized for the windows stdin */
  784. private void
  785. win_pipe_init(void)
  786. {    if ( gs_stream_stdin->procs.process == win_std_read_process )
  787.         gs_stream_stdin->procs.process = win_pipe_read_process;
  788. }
  789.  
  790. private int
  791. win_pipe_read_process(stream_state *st, stream_cursor_read *ignore_pr,
  792.   stream_cursor_write *pw, bool last)
  793. {    if ( pw->ptr < pw->limit )
  794.     {    int nread = win_pipe_read(pw->ptr + 1, 1);
  795.         if ( nread <= 0 )
  796.             return EOFC;
  797.         pw->ptr++;
  798.     }
  799.     return 1;
  800. }
  801.  
  802. /* get a block from gsview */
  803. private void
  804. win_pipe_request(void)
  805. {
  806.     MSG msg;
  807.     /* request another block */
  808.     if (pipe_lpbyte != (LPBYTE)NULL)
  809.         GlobalUnlock(pipe_hglobal);
  810.     if (pipe_hglobal != (HGLOBAL)NULL)
  811.         GlobalFree(pipe_hglobal);
  812.     pipe_hglobal = (HGLOBAL)NULL;
  813.     pipe_lpbyte = (LPBYTE)NULL;
  814.  
  815.     SendMessage(gsview_hwnd, WM_GSVIEW, PIPE_REQUEST, 0L);
  816.     /* wait for block */
  817.     /* don't use PeekMessage in a loop - would waste CPU cycles under WinNT */
  818.         while ((pipe_hglobal == (HGLOBAL)NULL)
  819.       && GetMessage(&msg, 0, 0, 0)) {
  820.         TranslateMessage(&msg);
  821.         DispatchMessage(&msg);
  822.     }
  823.     pipe_lpbyte = GlobalLock(pipe_hglobal);
  824.     pipe_count = *((WORD FAR *)pipe_lpbyte);
  825.     pipe_lpbyte += sizeof(WORD);
  826. }
  827.  
  828. /* pipe_read - similar to fread */
  829. private int
  830. win_pipe_read(char *ptr, unsigned int count)
  831. {
  832.     int n;
  833.     if (!pipe_count)
  834.         win_pipe_request();
  835.     if (!pipe_count)
  836.         return -1;
  837.     n = min(count, pipe_count);
  838.     memcpy(ptr, pipe_lpbyte, n);
  839.     pipe_lpbyte += n;
  840.     pipe_count -= n;
  841.     return n;
  842. }
  843.