gs16spl.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* Copyright (C) 1995, Russell Lang. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: gs16spl.c,v 1.4 2002/02/21 22:24:52 giles Exp $ */
  14. /* 16-bit access to print spooler from Win32s */
  15. /* by Russell Lang */
  16. /* 1995-11-23 */
  17. /*
  18. * Ghostscript produces printer specific output
  19. * which must be given to the print spooler.
  20. * Under Win16, the APIs OpenJob, WriteSpool etc. are used
  21. * Under Win32 and Windows 95/NT, the APIs OpenPrinter, WritePrinter etc.
  22. * are used.
  23. * Under Win32s, the 16-bit spooler APIs are not available, and the
  24. * 32-bit spooler APIs are not implemented.
  25. * This purpose of this application is to provide a means for the Win32s
  26. * version of Ghostscript to send output to the 16-bit spooler.
  27. */
  28. /*
  29. * Usage: gs16spl port filename
  30. *
  31. * filename will be sent to the spooler port.
  32. */
  33. #define STRICT
  34. #include <windows.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #define ID_TEXT 100
  39. /* documented in Device Driver Adaptation Guide */
  40. /* Prototypes taken from print.h */
  41. DECLARE_HANDLE(HPJOB);
  42. HPJOB WINAPI OpenJob(LPSTR, LPSTR, HPJOB);
  43. int WINAPI StartSpoolPage(HPJOB);
  44. int WINAPI EndSpoolPage(HPJOB);
  45. int WINAPI WriteSpool(HPJOB, LPSTR, int);
  46. int WINAPI CloseJob(HPJOB);
  47. int WINAPI DeleteJob(HPJOB, int);
  48. int WINAPI WriteDialog(HPJOB, LPSTR, int);
  49. int WINAPI DeleteSpoolPage(HPJOB);
  50. #define MAXSTR 256
  51. #define PRINT_BUF_SIZE 16384
  52. HPJOB hJob;
  53. HWND hwndspl;
  54. DLGPROC lpfnSpoolProc;
  55. HINSTANCE phInstance;
  56. char port[MAXSTR];
  57. char filename[MAXSTR];
  58. char error_message[MAXSTR];
  59. int error;
  60. char szAppName[] = "GS Win32s/Win16 spooler";
  61. /* returns TRUE on success, FALSE on failure */
  62. int
  63. spoolfile(char *portname, char *filename)
  64. {
  65. FILE *f;
  66. char *buffer;
  67. char pcdone[64];
  68. long ldone;
  69. long lsize;
  70. int count;
  71. MSG msg;
  72. if ((*portname == '\0') || (*filename == '\0')) {
  73. strcpy(error_message, "Usage: gs16spl port filename");
  74. return FALSE;
  75. }
  76. if ((buffer = malloc(PRINT_BUF_SIZE)) == (char *)NULL)
  77. return FALSE;
  78. if ((f = fopen(filename, "rb")) == (FILE *) NULL) {
  79. sprintf(error_message, "Can't open %s", filename);
  80. free(buffer);
  81. return FALSE;
  82. }
  83. fseek(f, 0L, SEEK_END);
  84. lsize = ftell(f);
  85. if (lsize <= 0)
  86. lsize = 1;
  87. fseek(f, 0L, SEEK_SET);
  88. ldone = 0;
  89. hJob = OpenJob(portname, filename, (HDC) NULL);
  90. switch ((int)hJob) {
  91. case SP_APPABORT:
  92. case SP_ERROR:
  93. case SP_OUTOFDISK:
  94. case SP_OUTOFMEMORY:
  95. case SP_USERABORT:
  96. fclose(f);
  97. free(buffer);
  98. return FALSE;
  99. }
  100. if (StartSpoolPage(hJob) < 0)
  101. error = TRUE;
  102. while (!error
  103. && (count = fread(buffer, 1, PRINT_BUF_SIZE, f)) != 0) {
  104. if (WriteSpool(hJob, buffer, count) < 0)
  105. error = TRUE;
  106. ldone += count;
  107. sprintf(pcdone, "%d%% written to %s", (int)(ldone * 100 / lsize), portname);
  108. SetWindowText(GetDlgItem(hwndspl, ID_TEXT), pcdone);
  109. while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
  110. TranslateMessage(&msg);
  111. DispatchMessage(&msg);
  112. }
  113. }
  114. free(buffer);
  115. fclose(f);
  116. EndSpoolPage(hJob);
  117. if (error)
  118. DeleteJob(hJob, 0);
  119. else
  120. CloseJob(hJob);
  121. return !error;
  122. }
  123. /* Modeless dialog box - main window */
  124. BOOL CALLBACK _export
  125. SpoolDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  126. {
  127. switch (message) {
  128. case WM_INITDIALOG:
  129. SetWindowText(hDlg, szAppName);
  130. return TRUE;
  131. case WM_COMMAND:
  132. switch (LOWORD(wParam)) {
  133. case IDCANCEL:
  134. error = TRUE;
  135. DestroyWindow(hDlg);
  136. EndDialog(hDlg, 0);
  137. PostQuitMessage(0);
  138. return TRUE;
  139. }
  140. }
  141. return FALSE;
  142. }
  143. void
  144. init_window(LPSTR cmdline)
  145. {
  146. LPSTR s;
  147. char *d;
  148. s = cmdline;
  149. /* skip leading spaces */
  150. while (*s && *s == ' ')
  151. s++;
  152. /* copy port name */
  153. d = port;
  154. while (*s && *s != ' ')
  155. *d++ = *s++;
  156. *d = '\0';
  157. /* skip spaces */
  158. while (*s && *s == ' ')
  159. s++;
  160. /* copy port name */
  161. d = filename;
  162. while (*s && *s != ' ')
  163. *d++ = *s++;
  164. *d = '\0';
  165. lpfnSpoolProc = (DLGPROC) MakeProcInstance((FARPROC) SpoolDlgProc, phInstance);
  166. hwndspl = CreateDialog(phInstance, "SpoolDlgBox", HWND_DESKTOP, lpfnSpoolProc);
  167. return;
  168. }
  169. int PASCAL
  170. WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int cmdShow)
  171. {
  172. MSG msg;
  173. phInstance = hInstance;
  174. init_window(lpszCmdLine);
  175. ShowWindow(hwndspl, cmdShow);
  176. if (!spoolfile(port, filename)) {
  177. /* wait, showing error message */
  178. SetWindowText(GetDlgItem(hwndspl, ID_TEXT), error_message);
  179. while (GetMessage(&msg, (HWND) NULL, 0, 0)) {
  180. TranslateMessage(&msg);
  181. DispatchMessage(&msg);
  182. }
  183. }
  184. DestroyWindow(hwndspl);
  185. FreeProcInstance((FARPROC) lpfnSpoolProc);
  186. return 0;
  187. }