Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

380 lines
9.5 KiB

  1. /* File: D:\wacker\tdll\prnecho.c (Created: 24-Jan-1994)
  2. *
  3. * Copyright 1994 by Hilgraeve Inc. -- Monroe, MI
  4. * All rights reserved
  5. *
  6. * $Revision: 6 $
  7. * $Date: 3/15/02 12:16p $
  8. */
  9. //#define DEBUGSTR 1
  10. #include <windows.h>
  11. #pragma hdrstop
  12. #include "stdtyp.h"
  13. #include "session.h"
  14. #include "assert.h"
  15. #include "print.h"
  16. #include "print.hh"
  17. #include "tdll.h"
  18. #include "htchar.h"
  19. #include "mc.h"
  20. //#define DEBUGSTR
  21. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  22. * FUNCTION:
  23. * printEchoChar
  24. *
  25. * DESCRIPTION:
  26. * Prints a single character by formating it for printString().
  27. *
  28. * ARGUMENTS:
  29. * HPRINT hPrint - The external print handle.
  30. * ECHAR tChar - The character to print.
  31. *
  32. * RETURNS:
  33. * TRUE = OK, FALSE = error
  34. *
  35. */
  36. int printEchoChar(const HPRINT hPrint, const ECHAR tChar)
  37. {
  38. HHPRINT hhPrint = (HHPRINT)hPrint;
  39. TCHAR *pszTemp;
  40. int nByteCnt;
  41. if ((hhPrint->nFlags & PRNECHO_IS_ON) == 0 ||
  42. (hhPrint->nFlags & PRNECHO_PAUSE))
  43. return FALSE;
  44. /*
  45. if ((hhPrint->nFlags & PRNECHO_CHARS) == 0)
  46. return FALSE;
  47. */
  48. // A bit of history. Originally I just sent the character along
  49. // to the PrintString function. However, this generated a large
  50. // metafile that caused Windows to crash and burn. Microsoft
  51. // suggested banding but that was more work than I wanted. So the
  52. // kludge fix is to gather them-there characters into an array and
  53. // flush-um out when the time comes.
  54. hhPrint->achPrnEchoLine[hhPrint->nLnIdx++] = tChar;
  55. if (tChar == ETEXT('\n') ||
  56. hhPrint->nLnIdx >= (int)((sizeof(hhPrint->achPrnEchoLine) / sizeof(ECHAR)) - 1))
  57. {
  58. // Force LF.
  59. //
  60. hhPrint->achPrnEchoLine[hhPrint->nLnIdx-1] = ETEXT('\n');
  61. // Convert over to a MBCS string for printing
  62. pszTemp = malloc(sizeof(hhPrint->achPrnEchoLine)+sizeof(ECHAR));
  63. nByteCnt = CnvrtECHARtoMBCS(pszTemp, sizeof(hhPrint->achPrnEchoLine),
  64. hhPrint->achPrnEchoLine,
  65. hhPrint->nLnIdx * sizeof(ECHAR));
  66. // Make sure that the string is NULL terminated.
  67. pszTemp[nByteCnt] = TEXT('\0');
  68. DbgOutStr("%s",pszTemp,0,0,0,0);
  69. printString(hhPrint, pszTemp, StrCharGetByteCount(pszTemp));
  70. free(pszTemp);
  71. pszTemp = NULL;
  72. hhPrint->nLnIdx = 0;
  73. ECHAR_Fill(hhPrint->achPrnEchoLine,
  74. ETEXT('\0'),
  75. sizeof(hhPrint->achPrnEchoLine)/sizeof(ECHAR));
  76. }
  77. return TRUE;
  78. }
  79. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  80. * FUNCTION:
  81. * printEchoString
  82. *
  83. * DESCRIPTION:
  84. * External interface to print a string.
  85. *
  86. * ARGUMENTS:
  87. * HPRINT hPrint - The external print handle.
  88. * ECHAR *achPrintStr - A pointer to an array of characters string.
  89. * Include "\r\n" to finish a line.
  90. * int iLen - The number of characters to print.
  91. *
  92. * RETURNS:
  93. * TRUE on success.
  94. *
  95. */
  96. int printEchoString(HPRINT hPrint, ECHAR *achPrintStr, int iLen)
  97. {
  98. HHPRINT hhPrint = (HHPRINT)hPrint;
  99. TCHAR *pszTemp;
  100. int nByteCnt;
  101. int nRet = 0;
  102. if ((hhPrint->nFlags & PRNECHO_IS_ON) == 0 ||
  103. (hhPrint->nFlags & PRNECHO_PAUSE))
  104. return FALSE;
  105. if ((hhPrint->nFlags & PRNECHO_LINES) == 0)
  106. return FALSE;
  107. pszTemp = malloc((unsigned int)iLen * sizeof(ECHAR));
  108. nByteCnt = CnvrtECHARtoMBCS(pszTemp, (unsigned int)iLen * sizeof(ECHAR),
  109. achPrintStr, (unsigned int)iLen * sizeof(ECHAR));
  110. // Make sure that the string is NULL terminated.
  111. pszTemp[nByteCnt] = ETEXT('\0');
  112. DbgOutStr("%s",pszTemp,0,0,0,0);
  113. nRet = printString(hhPrint, pszTemp, nByteCnt);
  114. free(pszTemp);
  115. pszTemp = NULL;
  116. return nRet;
  117. }
  118. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  119. * FUNCTION:
  120. * printEchoScreen
  121. *
  122. * DESCRIPTION:
  123. * Really the same func as printEchoString but checks if we are
  124. * in screen mode. This keeps the PrintEchoString from intermixing
  125. * lines in the output.
  126. *
  127. * ARGUMENTS:
  128. *
  129. * HPRINT hPrint - The external print handle.
  130. * ECHAR *pszPrintStr - A pointer to NULL terminated string.
  131. * Include "\r\n" to finish a line.
  132. * RETURNS:
  133. * TRUE = OK, FALSE = error.
  134. *
  135. */
  136. int printEchoScreen(HPRINT hPrint, ECHAR *achPrintStr, int iLen)
  137. {
  138. HHPRINT hhPrint = (HHPRINT)hPrint;
  139. TCHAR *pszTemp;
  140. int nByteCnt;
  141. int nRet;
  142. if ((hhPrint->nFlags & PRNECHO_IS_ON) == 0 ||
  143. (hhPrint->nFlags & PRNECHO_PAUSE))
  144. return FALSE;
  145. if ((hhPrint->nFlags & PRNECHO_SCREENS) == 0)
  146. return FALSE;
  147. pszTemp = malloc((unsigned int)iLen * sizeof(ECHAR));
  148. nByteCnt = CnvrtECHARtoMBCS(pszTemp, (unsigned int)iLen * sizeof(ECHAR),
  149. achPrintStr, (unsigned int)iLen * sizeof(ECHAR));
  150. // Make sure that the string is NULL terminated.
  151. pszTemp[nByteCnt] = ETEXT('\0');
  152. DbgOutStr("%s",pszTemp,0,0,0,0);
  153. nRet = printString(hhPrint, pszTemp, (unsigned int)nByteCnt);
  154. free(pszTemp);
  155. pszTemp = NULL;
  156. return nRet;
  157. }
  158. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  159. * FUNCTION:
  160. * printEchoStart
  161. *
  162. * DESCRIPTION:
  163. * The print echo functions are general purpose and not limited to the
  164. * print echo function. However, we still need to set flags so that
  165. * printing takes place. This function was added so that host directed
  166. * printing which uses the print echo functions could get started.
  167. *
  168. * ARGUMENTS:
  169. * HPRINT hPrint - The external Print handle.
  170. *
  171. * RETURNS:
  172. * TRUE If successful,
  173. * FALSE If the external print handle is bad.
  174. *
  175. */
  176. int printEchoStart(HPRINT hPrint)
  177. {
  178. HHPRINT hhPrint = (HHPRINT)hPrint;
  179. if (hPrint == 0)
  180. return FALSE;
  181. hhPrint->nFlags |= PRNECHO_IS_ON;
  182. hhPrint->nFlags &= ~PRNECHO_PAUSE;
  183. return TRUE;
  184. }
  185. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  186. * FUNCTION:
  187. * printEchoClose
  188. *
  189. * DESCRIPTION:
  190. * This function cleans up a print operation by closing the printer DC
  191. * and forcing the remaining pages out to the print spooler.
  192. *
  193. * ARGUMENTS:
  194. * HPRINT hPrint - The external Print handle.
  195. *
  196. * RETURNS:
  197. * TRUE
  198. *
  199. */
  200. int printEchoClose(HPRINT hPrint)
  201. {
  202. HHPRINT hhPrint = (HHPRINT)hPrint;
  203. TCHAR *pszTemp;
  204. int nByteCnt;
  205. //
  206. // if there are chars in the buffer that have never been sent to the printer
  207. // flush them now.
  208. //
  209. if (hhPrint->nLnIdx > 0)
  210. {
  211. // Convert over to a MBCS string for printing
  212. pszTemp = malloc(sizeof(hhPrint->achPrnEchoLine)+sizeof(ECHAR));
  213. nByteCnt = CnvrtECHARtoMBCS(pszTemp, sizeof(hhPrint->achPrnEchoLine),
  214. hhPrint->achPrnEchoLine,
  215. hhPrint->nLnIdx * sizeof(ECHAR));
  216. // Make sure that the string is NULL terminated.
  217. pszTemp[nByteCnt] = ETEXT('\0');
  218. DbgOutStr("%s",pszTemp,0,0,0,0);
  219. printString(hhPrint, pszTemp, StrCharGetByteCount(pszTemp));
  220. free(pszTemp);
  221. pszTemp = NULL;
  222. hhPrint->nLnIdx = 0;
  223. ECHAR_Fill(hhPrint->achPrnEchoLine,
  224. ETEXT('\0'),
  225. sizeof(hhPrint->achPrnEchoLine)/sizeof(ECHAR));
  226. }
  227. if (hhPrint->hDC)
  228. {
  229. if (hhPrint->nStatus >= 0)
  230. {
  231. hhPrint->nStatus = EndPage(hhPrint->hDC);
  232. if (hhPrint->nStatus >= 0)
  233. hhPrint->nStatus = EndDoc(hhPrint->hDC);
  234. DbgOutStr("EndPage/EndDoc\r\n", 0, 0, 0, 0, 0);
  235. }
  236. printCtrlDeleteDC(hPrint);
  237. }
  238. // hhPrint->nFlags &= ~(PRNECHO_IS_ON | PRNECHO_PAUSE);
  239. if (hhPrint->nStatus < 0)
  240. NotifyClient(hhPrint->hSession,
  241. EVENT_PRINT_ERROR,
  242. (WORD)hhPrint->nStatus);
  243. return TRUE;
  244. }
  245. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  246. * FUNCTION:
  247. * PrintEchoRaw
  248. *
  249. * DESCRIPTION:
  250. * Fires data directly at the printing avoiding the print driver.
  251. * Certain modes of the DEC emulators use this.
  252. *
  253. * ARGUMENTS:
  254. * HPRINT hPrint - The external print handle.
  255. * ECHAR *pszPrintStr - The null terminated string to print.
  256. *
  257. * RETURNS:
  258. * TRUE if the string was printed successfully, otherwise
  259. * FALSE
  260. *
  261. */
  262. int printEchoRaw(HPRINT hPrint, ECHAR *pszPrintStr, int nLen)
  263. {
  264. HHPRINT hhPrint = (HHPRINT)hPrint;
  265. TCHAR ach[1024];
  266. assert( nLen <= 1024 );
  267. if (hhPrint->hDC == 0)
  268. {
  269. if (printOpenDC(hhPrint) == FALSE)
  270. {
  271. printEchoClose((HPRINT)hhPrint);
  272. return FALSE;
  273. }
  274. }
  275. if (hhPrint->nStatus > 0)
  276. {
  277. char * pBuffer;
  278. short sLength;
  279. CnvrtECHARtoMBCS(ach, sizeof(ach), pszPrintStr, (unsigned int)nLen);
  280. pBuffer = (char*) malloc( nLen + sizeof(short) + 1);
  281. sLength = (short)nLen;
  282. MemCopy( pBuffer, &sLength, sizeof( short ));
  283. if (nLen)
  284. MemCopy( pBuffer + sizeof(short), &ach, nLen );
  285. pBuffer[nLen + sizeof( short )] = '\0';
  286. hhPrint->nStatus = Escape( hhPrint->hDC, PASSTHROUGH, 0, pBuffer, NULL );
  287. free( pBuffer );
  288. pBuffer = NULL;
  289. //
  290. // if pasthrough fails then send the data through the print driver
  291. //
  292. if ( hhPrint->nStatus < 0 )
  293. {
  294. printEchoString(hPrint, pszPrintStr, nLen);
  295. }
  296. }
  297. return TRUE;
  298. }
  299. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  300. * printEchoLine
  301. *
  302. * DESCRIPTION:
  303. * Adds CR LF to specified line of text and prints the line.
  304. *
  305. *
  306. * ARGUMENTS:
  307. * hPrint - The external print handle.
  308. * pachLine - A pointer to the text to print.
  309. * iLen - The number of characters pointed to by pachLine.
  310. *
  311. * RETURNS:
  312. * nothing
  313. */
  314. void printEchoLine(const HPRINT hPrint, ECHAR *pachLine, int iLen)
  315. {
  316. ECHAR aech[256];
  317. printEchoString(hPrint, pachLine, iLen);
  318. CnvrtMBCStoECHAR(aech, sizeof(aech), TEXT("\r\n"),
  319. StrCharGetByteCount(TEXT("\r\n")));
  320. printEchoString(hPrint, aech, sizeof(ECHAR) * 2);
  321. return;
  322. }