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.

495 lines
15 KiB

  1. /*=============================================================================
  2. * FILENAME: command.cpp
  3. * Copyright (C) 1996-1999 HDE, Inc. All Rights Reserved. HDE Confidential.
  4. * Copyright (C) 1999 NEC Technologies, Inc. All Rights Reserved.
  5. *
  6. * DESCRIPTION: Support for OEMCommand function to interject additional
  7. * postscript commands into the postscript stream.
  8. * NOTES:
  9. *=============================================================================
  10. */
  11. #include "precomp.h"
  12. #include <windows.h>
  13. #include <WINDDI.H>
  14. #include <PRINTOEM.H>
  15. #include <winspool.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <strsafe.h>
  19. #include "nc46nt.h"
  20. #include "oemps.h"
  21. /******************************************************************************
  22. * DESCRIPTION: Get *JUST* filename from Full path format data
  23. *****************************************************************************/
  24. extern "C"
  25. void GetFileName(char *FULL)
  26. {
  27. char work[NEC_DOCNAME_BUF_LEN+2], *pwork, *plast;
  28. int i, j;
  29. strncpy(work, FULL, NEC_DOCNAME_BUF_LEN+1); // #517724: PREFAST
  30. work[NEC_DOCNAME_BUF_LEN+1] = '\0'; // force terminate by '\0'
  31. j = strlen(work);
  32. for(plast = pwork = work; pwork < work + j; ++pwork)
  33. {
  34. if(*pwork == '\\') plast = pwork+1;
  35. }
  36. // Destination buffer size is smaller than 'work' buffer.
  37. strncpy(FULL, plast, NEC_DOCNAME_BUF_LEN);
  38. }
  39. /******************************************************************************
  40. * DESCRIPTION: Exported function for OEM plug-in. Required name and parameters
  41. * from the OEM plug-in spectification.
  42. * file must be exported in .def file
  43. *****************************************************************************/
  44. extern "C"
  45. DWORD APIENTRY OEMCommand( PDEVOBJ pDevObj, DWORD dwIndex,
  46. PVOID pData, DWORD cbSize )
  47. {
  48. char *pMem;
  49. char szUserName[NEC_USERNAME_BUF_LEN+2];
  50. PDRVPROCS pProcs;
  51. DWORD dwRv = ERROR_NOT_SUPPORTED;
  52. DWORD dwNeeded = 0;
  53. DWORD dwCOptions = 0;
  54. DWORD dwCbRet;
  55. BOOL bRv = 0;
  56. int NumCopies = 1;
  57. POEMPDEV poempdev = (POEMPDEV) pDevObj->pdevOEM;
  58. if( pDevObj == NULL )
  59. return( dwRv );
  60. if( (pProcs = (PDRVPROCS)pDevObj->pDrvProcs) == NULL )
  61. return( dwRv );
  62. switch( dwIndex )
  63. {
  64. case PSINJECT_BEGINSTREAM:
  65. pMem = (char*)EngAllocMem(FL_ZERO_MEMORY, NEC_DOCNAME_BUF_LEN, OEM_SIGNATURE );
  66. if( pMem != NULL )
  67. {
  68. POEMDEV pOEMDM = (POEMDEV)pDevObj->pOEMDM;
  69. // convert username from unicode string to ascii char string
  70. #ifdef USERMODE_DRIVER
  71. WideCharToMultiByte(CP_THREAD_ACP,
  72. WC_NO_BEST_FIT_CHARS,
  73. pOEMDM->szUserName,
  74. wcslen(pOEMDM->szUserName) * 2 + 2,
  75. szUserName,
  76. NEC_USERNAME_BUF_LEN,
  77. NULL,
  78. NULL);
  79. #else // !USERMODE_DRIVER
  80. EngUnicodeToMultiByteN( szUserName, NEC_USERNAME_BUF_LEN, &dwCbRet,
  81. pOEMDM->szUserName,
  82. // NEC_USERNAME_BUF_LEN * sizeof( WCHAR ) );
  83. wcslen(pOEMDM->szUserName) * 2 + 2);
  84. #endif // USERMODE_DRIVER
  85. cbSize = 50;
  86. if( pProcs->DrvGetDriverSetting != NULL )
  87. {
  88. dwRv = pProcs->DrvGetDriverSetting( pDevObj, "NCSpooler", pMem, cbSize, &dwNeeded, &dwCOptions );
  89. if( strcmp( pMem, "True" ) == 0 )
  90. {
  91. // output PS-like header
  92. StringCbCopyA(pMem, NEC_DOCNAME_BUF_LEN,
  93. "%!PS-Adobe-3.0\r\n% Spooled PostScript job\r\n{\r\n {\r\n 1183615869 internaldict begin\r\n (Job ID is) print \r\n <<\r\n"
  94. );
  95. if( pProcs->DrvWriteSpoolBuf != NULL )
  96. pProcs->DrvWriteSpoolBuf( pDevObj, pMem, strlen(pMem) );
  97. // output JobName
  98. StringCbCopyA(pMem, NEC_DOCNAME_BUF_LEN, " /JobName (");
  99. if( pProcs->DrvWriteSpoolBuf != NULL )
  100. pProcs->DrvWriteSpoolBuf( pDevObj, pMem, strlen(pMem) );
  101. strncpy(pMem, poempdev->szDocName, NEC_DOCNAME_BUF_LEN);
  102. // strcpy(pMem, "c:\\test\\test\\test\\simulate.dat"); // for Full Path Simulation
  103. GetFileName(pMem);
  104. // check document name
  105. //if(strcmp(pMem, "") == 0) strcpy(pMem, "No information");
  106. if( pProcs->DrvWriteSpoolBuf != NULL )
  107. pProcs->DrvWriteSpoolBuf( pDevObj, pMem, strlen(pMem) );
  108. StringCbCopyA(pMem, NEC_DOCNAME_BUF_LEN, ")\r\n");
  109. if( pProcs->DrvWriteSpoolBuf != NULL )
  110. pProcs->DrvWriteSpoolBuf( pDevObj, pMem, strlen(pMem) );
  111. // output Owner
  112. //sprintf(pMem," /Owner (%s)\r\n",szUserName);
  113. StringCbCopyA(pMem, NEC_DOCNAME_BUF_LEN, " /Owner (");
  114. if( pProcs->DrvWriteSpoolBuf != NULL )
  115. pProcs->DrvWriteSpoolBuf( pDevObj, pMem, strlen(pMem) );
  116. strncpy(pMem, szUserName, NEC_DOCNAME_BUF_LEN);
  117. // check szUserName
  118. // if(strcmp(pMem, "") == 0) strcpy(pMem, "No information");
  119. if( pProcs->DrvWriteSpoolBuf != NULL )
  120. pProcs->DrvWriteSpoolBuf( pDevObj, pMem, strlen(pMem) );
  121. StringCbCopyA(pMem, NEC_DOCNAME_BUF_LEN, ")\r\n");
  122. if( pProcs->DrvWriteSpoolBuf != NULL )
  123. pProcs->DrvWriteSpoolBuf( pDevObj, pMem, strlen(pMem) );
  124. // Get and Modify NumCopies
  125. cbSize = 50;
  126. if( pProcs->DrvGetDriverSetting != NULL )
  127. {
  128. dwRv = pProcs->DrvGetDriverSetting( pDevObj, "NCCollate", pMem, cbSize, &dwNeeded, &dwCOptions );
  129. if( strcmp( pMem, "True" ) == 0 )
  130. {
  131. NumCopies = pDevObj->pPublicDM->dmCopies;
  132. pDevObj->pPublicDM->dmCopies = 1;
  133. }
  134. }
  135. // output NumCopies
  136. //sprintf(pMem," /NumCopies %d\r\n",NumCopies);
  137. StringCbCopyA(pMem, NEC_DOCNAME_BUF_LEN, " /NumCopies ");
  138. if( pProcs->DrvWriteSpoolBuf != NULL )
  139. pProcs->DrvWriteSpoolBuf( pDevObj, pMem, strlen(pMem) );
  140. _itoa(NumCopies, pMem, 10);
  141. if( pProcs->DrvWriteSpoolBuf != NULL )
  142. pProcs->DrvWriteSpoolBuf( pDevObj, pMem, strlen(pMem) );
  143. StringCbCopyA(pMem, NEC_DOCNAME_BUF_LEN, "\r\n");
  144. if( pProcs->DrvWriteSpoolBuf != NULL )
  145. pProcs->DrvWriteSpoolBuf( pDevObj, pMem, strlen(pMem) );
  146. // Put /Collate
  147. cbSize = 50;
  148. if( pProcs->DrvGetDriverSetting != NULL )
  149. {
  150. dwRv = pProcs->DrvGetDriverSetting( pDevObj, "NCCollate", pMem, cbSize, &dwNeeded, &dwCOptions );
  151. if( strcmp( pMem, "True" ) == 0 )
  152. {
  153. StringCbCopyA(pMem, NEC_DOCNAME_BUF_LEN, " /Collate true\r\n");
  154. if( pProcs->DrvWriteSpoolBuf != NULL )
  155. pProcs->DrvWriteSpoolBuf( pDevObj, pMem, strlen(pMem) );
  156. }
  157. else
  158. {
  159. StringCbCopyA(pMem, NEC_DOCNAME_BUF_LEN, " /Collate false\r\n");
  160. if( pProcs->DrvWriteSpoolBuf != NULL )
  161. pProcs->DrvWriteSpoolBuf( pDevObj, pMem, strlen(pMem) );
  162. }
  163. }
  164. // Put /Banner
  165. cbSize = 50;
  166. if( pProcs->DrvGetDriverSetting != NULL )
  167. {
  168. dwRv = pProcs->DrvGetDriverSetting( pDevObj, "NCBanner", pMem, cbSize, &dwNeeded, &dwCOptions );
  169. if( strcmp( pMem, "True" ) == 0 )
  170. {
  171. StringCbCopyA(pMem, NEC_DOCNAME_BUF_LEN, " /Banner true\r\n");
  172. if( pProcs->DrvWriteSpoolBuf != NULL )
  173. pProcs->DrvWriteSpoolBuf( pDevObj, pMem, strlen(pMem) );
  174. }
  175. else
  176. {
  177. StringCbCopyA(pMem, NEC_DOCNAME_BUF_LEN, " /Banner false\r\n");
  178. if( pProcs->DrvWriteSpoolBuf != NULL )
  179. pProcs->DrvWriteSpoolBuf( pDevObj, pMem, strlen(pMem) );
  180. }
  181. }
  182. // Put /DeleteJob
  183. cbSize = 50;
  184. if( pProcs->DrvGetDriverSetting != NULL )
  185. {
  186. dwRv = pProcs->DrvGetDriverSetting( pDevObj, "NCJobpreview", pMem, cbSize, &dwNeeded, &dwCOptions );
  187. if( strcmp( pMem, "True" ) == 0 )
  188. {
  189. StringCbCopyA(pMem, NEC_DOCNAME_BUF_LEN, " /DeleteJob false\r\n");
  190. if( pProcs->DrvWriteSpoolBuf != NULL )
  191. pProcs->DrvWriteSpoolBuf( pDevObj, pMem, strlen(pMem) );
  192. }
  193. else
  194. {
  195. StringCbCopyA(pMem, NEC_DOCNAME_BUF_LEN, " /DeleteJob true\r\n");
  196. if( pProcs->DrvWriteSpoolBuf != NULL )
  197. pProcs->DrvWriteSpoolBuf( pDevObj, pMem, strlen(pMem) );
  198. }
  199. }
  200. // Put /HoldJob
  201. cbSize = 50;
  202. if( pProcs->DrvGetDriverSetting != NULL )
  203. {
  204. dwRv = pProcs->DrvGetDriverSetting( pDevObj, "NCHoldjob", pMem, cbSize, &dwNeeded, &dwCOptions );
  205. if( strcmp( pMem, "True" ) == 0 )
  206. {
  207. StringCbCopyA(pMem, NEC_DOCNAME_BUF_LEN, " /HoldJob true\r\n");
  208. if( pProcs->DrvWriteSpoolBuf != NULL )
  209. pProcs->DrvWriteSpoolBuf( pDevObj, pMem, strlen(pMem) );
  210. }
  211. else
  212. {
  213. StringCbCopyA(pMem, NEC_DOCNAME_BUF_LEN, " /HoldJob false\r\n");
  214. if( pProcs->DrvWriteSpoolBuf != NULL )
  215. pProcs->DrvWriteSpoolBuf( pDevObj, pMem, strlen(pMem) );
  216. }
  217. }
  218. // Put /Priority
  219. cbSize = 50;
  220. if( pProcs->DrvGetDriverSetting != NULL )
  221. {
  222. dwRv = pProcs->DrvGetDriverSetting( pDevObj, "NCPriority", pMem, cbSize, &dwNeeded, &dwCOptions );
  223. if( strcmp( pMem, "P1" ) == 0 )
  224. {
  225. StringCbCopyA(pMem, NEC_DOCNAME_BUF_LEN, " /Priority 1\r\n");
  226. if( pProcs->DrvWriteSpoolBuf != NULL )
  227. pProcs->DrvWriteSpoolBuf( pDevObj, pMem, strlen(pMem) );
  228. }
  229. if( strcmp( pMem, "P2" ) == 0 )
  230. {
  231. StringCbCopyA(pMem, NEC_DOCNAME_BUF_LEN, " /Priority 2\r\n");
  232. if( pProcs->DrvWriteSpoolBuf != NULL )
  233. pProcs->DrvWriteSpoolBuf( pDevObj, pMem, strlen(pMem) );
  234. }
  235. if( strcmp( pMem, "P3" ) == 0 )
  236. {
  237. StringCbCopyA(pMem, NEC_DOCNAME_BUF_LEN, " /Priority 3\r\n");
  238. if( pProcs->DrvWriteSpoolBuf != NULL )
  239. pProcs->DrvWriteSpoolBuf( pDevObj, pMem, strlen(pMem) );
  240. }
  241. if( strcmp( pMem, "P4" ) == 0 )
  242. {
  243. StringCbCopyA(pMem, NEC_DOCNAME_BUF_LEN, " /Priority 4\r\n");
  244. if( pProcs->DrvWriteSpoolBuf != NULL )
  245. pProcs->DrvWriteSpoolBuf( pDevObj, pMem, strlen(pMem) );
  246. }
  247. if( strcmp( pMem, "P5" ) == 0 )
  248. {
  249. StringCbCopyA(pMem, NEC_DOCNAME_BUF_LEN, " /Priority 5\r\n");
  250. if( pProcs->DrvWriteSpoolBuf != NULL )
  251. pProcs->DrvWriteSpoolBuf( pDevObj, pMem, strlen(pMem) );
  252. }
  253. if( strcmp( pMem, "P6" ) == 0 )
  254. {
  255. StringCbCopyA(pMem, NEC_DOCNAME_BUF_LEN, " /Priority 6\r\n");
  256. if( pProcs->DrvWriteSpoolBuf != NULL )
  257. pProcs->DrvWriteSpoolBuf( pDevObj, pMem, strlen(pMem) );
  258. }
  259. if( strcmp( pMem, "P7" ) == 0 )
  260. {
  261. StringCbCopyA(pMem, NEC_DOCNAME_BUF_LEN, " /Priority 7\r\n");
  262. if( pProcs->DrvWriteSpoolBuf != NULL )
  263. pProcs->DrvWriteSpoolBuf( pDevObj, pMem, strlen(pMem) );
  264. }
  265. if( strcmp( pMem, "P8" ) == 0 )
  266. {
  267. StringCbCopyA(pMem, NEC_DOCNAME_BUF_LEN, " /Priority 8\r\n");
  268. if( pProcs->DrvWriteSpoolBuf != NULL )
  269. pProcs->DrvWriteSpoolBuf( pDevObj, pMem, strlen(pMem) );
  270. }
  271. if( strcmp( pMem, "P9" ) == 0 )
  272. {
  273. StringCbCopyA(pMem, NEC_DOCNAME_BUF_LEN, " /Priority 9\r\n");
  274. if( pProcs->DrvWriteSpoolBuf != NULL )
  275. pProcs->DrvWriteSpoolBuf( pDevObj, pMem, strlen(pMem) );
  276. }
  277. if( strcmp( pMem, "P10" ) == 0 )
  278. {
  279. StringCbCopyA(pMem, NEC_DOCNAME_BUF_LEN, " /Priority 10\r\n");
  280. if( pProcs->DrvWriteSpoolBuf != NULL )
  281. pProcs->DrvWriteSpoolBuf( pDevObj, pMem, strlen(pMem) );
  282. }
  283. }
  284. StringCbCopyA(pMem, NEC_DOCNAME_BUF_LEN,
  285. " >>\r\n spoolgetjobid dup == flush\r\n spooljobsubmit\r\n } stopped {end clear} if\r\n} exec\r\n");
  286. if( pProcs->DrvWriteSpoolBuf != NULL )
  287. pProcs->DrvWriteSpoolBuf( pDevObj, pMem, strlen(pMem) );
  288. }
  289. }
  290. dwRv = ERROR_SUCCESS;
  291. EngFreeMem( pMem );
  292. }
  293. break;
  294. }
  295. return( dwRv );
  296. }
  297. //
  298. // OEMStartDoc
  299. //
  300. extern "C"
  301. BOOL APIENTRY
  302. OEMStartDoc(
  303. SURFOBJ *pso,
  304. PWSTR pwszDocName,
  305. DWORD dwJobId
  306. )
  307. {
  308. PDEVOBJ pdevobj;
  309. POEMPDEV poempdev;
  310. HANDLE hPrinter;
  311. DWORD dwCbRet;
  312. JOB_INFO_1 *pJob;
  313. DWORD cbNeeded;
  314. pdevobj = (PDEVOBJ)pso->dhpdev;
  315. poempdev = (POEMPDEV)pdevobj->pdevOEM;
  316. //
  317. // Add DocName copy command here ( copy from pwszDocName to poempdev->szDocName )
  318. //
  319. if((NULL != pwszDocName) && (NULL != poempdev->szDocName))
  320. {
  321. #ifdef USERMODE_DRIVER
  322. WideCharToMultiByte(CP_THREAD_ACP,
  323. WC_NO_BEST_FIT_CHARS,
  324. pwszDocName,
  325. wcslen(pwszDocName) * 2 + 2,
  326. poempdev->szDocName,
  327. NEC_DOCNAME_BUF_LEN,
  328. NULL,
  329. NULL);
  330. //
  331. // W2K/Whistler's user mode driver does not pass the correct job name in pwszDocName
  332. // in case that the printer is connected to network port.
  333. // So, I need to get the job name from JOB_INFO_1 structure.
  334. //
  335. if (OpenPrinter(poempdev->pPrinterName, &hPrinter, NULL) != 0) {
  336. GetJob(hPrinter, dwJobId, 1, NULL, 0, &cbNeeded);
  337. pJob = (JOB_INFO_1 *)EngAllocMem(FL_ZERO_MEMORY, cbNeeded, OEM_SIGNATURE);
  338. if (NULL != pJob) {
  339. if (GetJob(hPrinter, dwJobId, 1, (LPBYTE)pJob, cbNeeded, &cbNeeded) != 0) {
  340. WideCharToMultiByte(CP_THREAD_ACP,
  341. WC_NO_BEST_FIT_CHARS,
  342. pJob->pDocument,
  343. wcslen(pJob->pDocument) * 2 + 2,
  344. poempdev->szDocName,
  345. NEC_DOCNAME_BUF_LEN,
  346. NULL,
  347. NULL);
  348. }
  349. EngFreeMem(pJob);
  350. }
  351. ClosePrinter(hPrinter);
  352. }
  353. #else // !USERMODE_DRIVER
  354. EngUnicodeToMultiByteN( poempdev->szDocName, NEC_DOCNAME_BUF_LEN, &dwCbRet,
  355. pwszDocName,
  356. // NEC_DOCNAME_BUF_LEN * sizeof( WCHAR ) );
  357. wcslen(pwszDocName) * 2 + 2);
  358. #endif // USERMODE_DRIVER
  359. }
  360. //
  361. // turn around to call PS
  362. //
  363. return (((PFN_DrvStartDoc)(poempdev->pfnPS[UD_DrvStartDoc])) (
  364. pso,
  365. pwszDocName,
  366. dwJobId));
  367. }
  368. //
  369. // OEMEndDoc
  370. //
  371. extern "C"
  372. BOOL APIENTRY
  373. OEMEndDoc(
  374. SURFOBJ *pso,
  375. FLONG fl
  376. )
  377. {
  378. PDEVOBJ pdevobj;
  379. POEMPDEV poempdev;
  380. pdevobj = (PDEVOBJ)pso->dhpdev;
  381. poempdev = (POEMPDEV)pdevobj->pdevOEM;
  382. //
  383. // turn around to call PS
  384. //
  385. return (((PFN_DrvEndDoc)(poempdev->pfnPS[UD_DrvEndDoc])) (
  386. pso,
  387. fl));
  388. }