source: trunk/cups_pdr/cups.c@ 13

Last change on this file since 13 was 13, checked in by Alex Taylor, 14 years ago

Import of CUPS.PDR sources.

File size: 35.2 KB
Line 
1#define LINT_ARGS /* argument checking enabled */
2
3#define INCL_DOS
4#define INCL_GPI
5#undef INCL_GPI
6#define INCL_DEV
7#define INCL_DOSMEMMGR /* Include standard OS/2 support */
8#define INCL_DOSMODULEMGR /* For DosLoadModule */
9#define INCL_DOSPROCESS
10#define INCL_GPILCIDS
11#define INCL_WINCOMMON /* Include Window Management support */
12#define INCL_WINDOWMGR
13#define INCL_WINSWITCHLIST
14#define INCL_WINPROGRAMLIST
15#define INCL_WINMENUS
16#define INCL_WINWINDOWMGR
17#define INCL_WINMESSAGEMGR
18#define INCL_WINDIALOGS
19#define INCL_WINSTATICS
20#define INCL_WINLISTBOXES
21#define INCL_WINMENUS
22#define INCL_WINSYS
23#define INCL_WINFRAMEMGR
24#define INCL_INCLWINACCELERATORS
25#define INCL_WINPOINTERS
26#define INCL_WINERRORS
27#define INCL_WINSHELLDATA
28
29#define INCL_WINTYPES
30#define INCL_WINACCELERATORS
31#define INCL_WINBUTTONS
32#define INCL_WINENTRYFIELDS
33#define INCL_WINRECTANGLES
34#define INCL_WINTIMER
35#define INCL_WINSCROLLBARS
36#define INCL_WINHEAP
37#define INCL_SHLERRORS
38#define INCL_WININPUT
39#define INCL_WINHELP
40#define INCL_WINSTDSPIN
41
42#define INCL_SPL
43#define INCL_SPLP
44#define INCL_SPLERRORS
45#define INCL_SHLERRORS
46#define INCL_DOSERRORS
47#define INCL_WINHOOKS
48
49int acrtused=1; /* Define variable to say this is a DLL */
50
51#include <os2.h>
52
53#include <stdlib.h>
54#include <string.h>
55#include <ctype.h>
56#include <stdarg.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <process.h>
60
61#include "cups.h"
62
63#define BIDI_SET_PORTDRV 0x00000019
64#define BIDI_Q_PORTDRV 0x00008019
65
66//
67// If port driver is not defined in INI file yet
68// assume it exists in the boot drive's \OS2\DLL directory
69//
70
71CHAR szDefaultPortDrvPath[] = { PATH_CUPS_PDR };
72
73
74//
75// Below definition of PORTNAMES structure should be defined in
76// common header file pmspl.h.
77//
78
79typedef struct _PORTNAMES
80{
81 PSZ pszPortName; /* -> name of port(ie "CUPS1") */
82 PSZ pszPortDesc; /* -> description of port(ie "CUPS Port 1") */
83} PORTNAMES, *PPORTNAMES;
84
85#if 0
86char * lprtok (char *string,char *control)
87{
88 unsigned char *str;
89 const unsigned char *ctrl = control;
90
91 unsigned char map[32];
92 int count;
93
94 static char *nextoken;
95
96 for (count = 0; count < 32; count++)
97 map[count] = 0;
98
99 do {
100 map[*ctrl >> 3] |= (1 << (*ctrl & 7));
101 } while (*ctrl++);
102
103 if (string)
104 str = string;
105 else
106 str = nextoken;
107
108 while ( (map[*str >> 3] & (1 << (*str & 7))) && *str )
109 str++;
110
111 string = str;
112
113 for ( ; *str ; str++ )
114 if ( map[*str >> 3] & (1 << (*str & 7)) ) {
115 *str++ = '\0';
116 break;
117 }
118
119 nextoken = str;
120
121 /* Determine if a token has been found. */
122 if ( string == str )
123 return NULL;
124 else
125 return string;
126}
127#else
128/* New version of lprtok() that can handle missing fields */
129char * lprtok (char *string,char *control)
130{
131 char *c;
132 static char *next;
133
134 if ( control == NULL ) return string;
135 if ( string == NULL ) string = next;
136 if ( string == NULL ) return NULL;
137
138 if (( c = strpbrk( string, control )) == NULL ) {
139 next = NULL;
140 }
141 else {
142 next = c+1;
143 *c = '\0';
144 }
145
146 return ( string );
147}
148#endif
149MRESULT EXPENTRY CommDlg( HWND hDlg, USHORT msg, MPARAM mp1, MPARAM mp2 )
150{
151 PLPRDATA pLprData;
152 ULONG ulTimeOut = 0 ;
153 CHAR szDesc[ STR_LEN_PORTDESC];
154 CHAR szTitle[ STR_LEN_TITLE + 1];
155 CHAR szTemp[ STR_LEN_PORTDESC];
156 USHORT i;
157 ULONG rc = 0;
158 PUCHAR token;
159
160 switch (msg)
161 {
162 case WM_INITDLG:
163 //WinSendDlgItemMsg(hDlg,ID_BINARY,BM_SETCHECK,MPFROM2SHORT(1,0),NULL);
164 pLprData = (PLPRDATA)mp2;
165 WinSetWindowULong (hDlg, QWL_USER, (ULONG)pLprData);
166 if (PrfQueryProfileString (HINI_SYSTEMPROFILE,
167 pLprData->pszAppName,
168 KEY_DESCRIPTION,
169 NULL,
170 (PSZ)szDesc,
171 STR_LEN_PORTDESC))
172 {
173 WinSetWindowText (WinWindowFromID (hDlg, (USHORT)IDD_CUPS),szDesc);
174 rc = WinLoadString(pLprData->hAB,
175 pLprData->hModule,
176 PDR_ID_PROPERTIES,
177 STR_LEN_PORTDESC, szTemp);
178 if (rc)
179 {
180 strcpy ( szTitle, pLprData->pszPortName );
181 strcat ( szTitle, " - " );
182 strcat ( szTitle, szTemp );
183 WinSetWindowText (hDlg, szTitle);
184 }
185 }
186 if (PrfQueryProfileString (HINI_SYSTEMPROFILE,
187 pLprData->pszAppName,
188 KEY_INITIALIZATION,
189 NULL,
190 szTemp,
191 sizeof(szTemp)))
192 {
193 i = 0;
194 token = lprtok(szTemp,"#");
195 while (token != NULL) {
196 switch(i) {
197 case 0:
198 WinSetDlgItemText(hDlg,ID_IP,token);
199 break;
200 case 1:
201 if (token[ strlen(token) - 1 ] == ';')
202 token[ strlen(token)-1 ] = '\0';
203 WinSetDlgItemText(hDlg,ID_CUPSQUEUE,token);
204 break;
205 /* case 2:
206 * if (token[ strlen(token) - 1 ] == ';')
207 * token[ strlen(token)-1 ] = '\0';
208 * WinSetDlgItemText(hDlg,ID_SPOOL_PATH,token);
209 * break;
210 */
211 }
212 i++;
213 token = lprtok(NULL,"#");
214 }
215 }
216 break;
217
218 case WM_COMMAND:
219 pLprData = (PLPRDATA)WinQueryWindowULong (hDlg, QWL_USER);
220 switch (SHORT1FROMMP(mp1))
221 {
222 case DID_OK:
223 WinQueryDlgItemText (hDlg, ID_IP, sizeof(szTemp), szTemp );
224 sprintf(pLprData->szSaveLprSetting,"%s",szTemp);
225 strncpy( szDesc, szTemp, STR_LEN_PORTDESC-1 );
226
227 WinQueryDlgItemText (hDlg, ID_CUPSQUEUE, sizeof(szTemp), szTemp );
228 strcat(pLprData->szSaveLprSetting,"#");
229 strcat(pLprData->szSaveLprSetting,szTemp);
230 if ( strlen(szTemp) > 0 ) {
231 strncat( szDesc, ":", STR_LEN_PORTDESC-1 );
232 strncat( szDesc, szTemp, STR_LEN_PORTDESC-1 );
233 }
234/*
235 * WinQueryDlgItemText (hDlg, ID_SPOOL_PATH, sizeof(szTemp), szTemp );
236 * strcat(pLprData->szSaveLprSetting,"#");
237 * strcat(pLprData->szSaveLprSetting,szTemp);
238 */
239 if (!PrfWriteProfileString (HINI_SYSTEMPROFILE,
240 pLprData->pszAppName,
241 KEY_INITIALIZATION,
242 pLprData->szSaveLprSetting))
243 WinDismissDlg(hDlg, MBID_CANCEL);
244
245 if (!PrfWriteProfileString (HINI_SYSTEMPROFILE,
246 APPNAME_PM_SPOOLER_PORT,
247 pLprData->pszPortName,
248 pLprData->szSaveLprSetting))
249 WinDismissDlg(hDlg, MBID_CANCEL);
250
251 if (!PrfWriteProfileString (HINI_SYSTEMPROFILE,
252 pLprData->pszAppName,
253 KEY_DESCRIPTION,
254 szDesc ))
255 WinDismissDlg(hDlg, MBID_CANCEL);
256
257 WinDismissDlg(hDlg, TRUE);
258 break;
259 case DID_CANCEL:
260 WinDismissDlg(hDlg, MBID_CANCEL);
261 break;
262 }
263 break;
264 default:
265 return WinDefDlgProc(hDlg, msg, mp1, mp2) ;
266 break;
267 }
268 return FALSE;
269}
270ULONG CalcStructLength ( HAB hab,
271 HMODULE hModule,
272 USHORT usID )
273{
274 ULONG cbRequired;
275 CHAR chString[STR_LEN_PORTDESC];
276
277 cbRequired = 0;
278
279 WinLoadString(hab, hModule, usID, STR_LEN_PORTDESC, chString);
280 cbRequired += strlen (chString) + 1;
281 WinLoadString(hab, hModule, (USHORT)(usID + 1), STR_LEN_PORTDESC, chString);
282 cbRequired += strlen (chString) + 1;
283 cbRequired += sizeof (PORTNAMES);
284 return(cbRequired);
285}
286
287ULONG CalcBufLength ( HAB hab,
288 HMODULE hModule )
289{
290 ULONG cbRequired;
291 USHORT usID;
292
293 cbRequired = 0;
294
295 /*
296 ** calculate length required to fit all the port info.
297 */
298 for (usID = PORT_ID_FIRST; usID <= PORT_ID_LAST; usID += 2)
299 {
300 cbRequired += CalcStructLength (hab, hModule, usID);
301 }
302
303 return(cbRequired);
304}
305
306ULONG NumPortsCanFit ( HAB hab,
307 HMODULE hModule,
308 ULONG cbBuf )
309{
310 ULONG cbRequired;
311 USHORT usID;
312 ULONG ulNumPort;
313
314 cbRequired = 0;
315 ulNumPort = 0;
316
317 /*
318 ** calculate how many ports we can fit in buf.
319 */
320 for (usID = PORT_ID_FIRST; usID <= PORT_ID_LAST; usID += 2)
321 {
322 cbRequired += CalcStructLength (hab, hModule, usID);
323 if (cbRequired > cbBuf)
324 {
325 return(ulNumPort);
326 }
327 ulNumPort++;
328 }
329
330 return(ulNumPort);
331}
332VOID CopyStruct ( HAB hab,
333 HMODULE hModule,
334 USHORT usID,
335 PCH pBuf,
336 PULONG pulBeginStruct,
337 PULONG pulBeginText )
338{
339 PPORTNAMES pPortNames;
340
341 pPortNames = (PPORTNAMES)(pBuf + *pulBeginStruct);
342 *pulBeginStruct += sizeof (PORTNAMES);
343
344 /*
345 ** copy port name in the structure
346 */
347 pPortNames->pszPortName = pBuf + *pulBeginText;
348 WinLoadString(hab, hModule, usID, STR_LEN_PORTDESC, pPortNames->pszPortName);
349 *pulBeginText += strlen (pPortNames->pszPortName) + 1;
350
351 /*
352 ** copy port description to the structure
353 */
354 pPortNames->pszPortDesc = pBuf + *pulBeginText;
355 WinLoadString(hab, hModule, usID, STR_LEN_PORTDESC, pPortNames->pszPortDesc);
356 *pulBeginText += strlen (pPortNames->pszPortDesc) + 1;
357}
358VOID CopyNPorts ( HAB hab,
359 HMODULE hModule,
360 PCH pBuf,
361 ULONG ulReturned )
362{
363 USHORT usID;
364 ULONG ulBeginText;
365 ULONG ulBeginStruct;
366
367 ulBeginText = ulReturned * sizeof (PORTNAMES);
368 ulBeginStruct = 0;
369
370 for (usID = PORT_ID_FIRST;
371 usID <= PORT_ID_LAST && ulReturned;
372 usID += 2, --ulReturned)
373 {
374 CopyStruct (hab, hModule, usID, pBuf, &ulBeginStruct, &ulBeginText);
375 }
376}
377
378ULONG OpenLprPortDlg ( HAB hab,
379 HMODULE hModule,
380 PSZ pszPortName,
381 PSZ pszAppName )
382{
383 LPRDATA LprData;
384
385 memset (&LprData, 0, sizeof (LPRDATA));
386 LprData.hAB = hab;
387 LprData.hModule = hModule;
388 LprData.pszPortName = pszPortName;
389 LprData.pszAppName = pszAppName;
390
391 WinDlgBox (HWND_DESKTOP,
392 HWND_DESKTOP,
393 (PFNWP)CommDlg,
394 (HMODULE)hModule,
395 IDD_CUPS,
396 &LprData);
397
398 return LprData.lfModified;
399}
400BOOL GenerateUniquePortName( PSZ pszPortName )
401{
402 BOOL fPortExists;
403 PSZ pszEndPortName;
404 USHORT i;
405 CHAR chPortData[STR_LEN_PORTNAME];
406
407 /*
408 ** Generate a unique port name by adding numbers to the
409 ** end of pszPortName
410 */
411 pszEndPortName = pszPortName + strlen( pszPortName );
412 i = 1;
413 fPortExists = TRUE;
414 while ( (i < MAX_PORTS) && fPortExists )
415 {
416 _itoa( i, pszEndPortName, 10);
417 fPortExists = PrfQueryProfileString (HINI_SYSTEMPROFILE,
418 APPNAME_PM_SPOOLER_PORT,
419 pszPortName,
420 NULL,
421 chPortData,
422 sizeof(chPortData) - 1);
423 i++;
424 }
425 return(!fPortExists);
426}
427BOOL GetPortDescription ( HAB hab,
428 HMODULE hModule,
429 PSZ pszPortName,
430 PSZ pszPortDesc )
431{
432 USHORT usID;
433 CHAR chBuf[STR_LEN_PORTDESC] = {0};
434
435 for (usID = PORT_ID_FIRST; usID <= PORT_ID_LAST; usID += 2)
436 {
437 WinLoadString(hab, hModule, usID, STR_LEN_PORTDESC, chBuf);
438 if (!strcmp (pszPortName, chBuf))
439 {
440 if ( WinLoadString(hab, hModule, usID+1, STR_LEN_PORTDESC, chBuf) ) {
441 strcpy (pszPortDesc, chBuf);
442 return(TRUE);
443 }
444 break;
445 }
446 }
447 return(FALSE);
448}
449APIRET APIENTRY SplPdEnumPort ( HAB hab,
450 PVOID pBuf,
451 ULONG cbBuf,
452 PULONG pulReturned,
453 PULONG pulTotal,
454 PULONG pcbRequired )
455
456{
457 HMODULE hModule;
458 ULONG ulBootDrive;
459 ULONG rcLoadMod;
460 CHAR szPathName[260];
461
462 if (!pulReturned ||
463 !pulTotal ||
464 !pcbRequired)
465 {
466 return(ERROR_INVALID_PARAMETER);
467 }
468
469 if (!pBuf && cbBuf)
470 {
471 return(ERROR_INVALID_PARAMETER);
472 }
473
474 DosQuerySysInfo (QSV_BOOT_DRIVE, QSV_BOOT_DRIVE, &ulBootDrive,
475 sizeof (ULONG));
476 szDefaultPortDrvPath[0] = (CHAR)(ulBootDrive + 'A' - 1);
477
478 PrfQueryProfileString (HINI_SYSTEMPROFILE,
479 "PM_PORT_DRIVER",
480 "CUPS",
481 szDefaultPortDrvPath,
482 szPathName,
483 256 );
484
485 rcLoadMod = DosLoadModule (NULL, 0, szPathName, &hModule);
486
487 if (cbBuf == 0L)
488 {
489 *pulReturned = 0;
490 *pcbRequired = CalcBufLength (hab, hModule);
491 *pulTotal = MAX_PORTS; /* Currently support CUPSx x = 1 .. 64 */
492 if (!rcLoadMod)
493 DosFreeModule (hModule);
494 return(ERROR_MORE_DATA);
495 }
496
497 /*
498 ** check number of ports info we can fit in supplied buffer
499 */
500 *pulTotal = MAX_PORTS; /* Currently support CUPSx x= 1 .. 64 */
501 *pcbRequired = CalcBufLength (hab, hModule);
502 *pulReturned = NumPortsCanFit (hab, hModule, cbBuf);
503
504 /*
505 ** return error if we can not fit one port.
506 */
507 if (!(*pulReturned))
508 {
509 if (!rcLoadMod)
510 DosFreeModule (hModule);
511 return(ERROR_INSUFFICIENT_BUFFER);
512 }
513
514 /*
515 ** copy all the ports which we can store in the pBuf
516 */
517 CopyNPorts (hab, hModule, (PCH)pBuf, *pulReturned);
518
519 /*
520 ** Free the module - we do not need it any more.
521 */
522 if (!rcLoadMod)
523 DosFreeModule (hModule);
524
525 /*
526 ** copy all the ports which we can store in the pBuf
527 */
528 if (*pulReturned < *pulTotal)
529 {
530 return(ERROR_MORE_DATA);
531 }
532
533 return(NO_ERROR);
534}
535APIRET APIENTRY SplPdInstallPort ( HAB hab,
536 PSZ pszPortName )
537{
538 CHAR chBuf[STR_LEN_PORTNAME];
539 CHAR chPortDesc[STR_LEN_PORTDESC];
540 ULONG ulBootDrive;
541 HMODULE hModule;
542 CHAR szPathName[260]; /* will contain full path to this port driver */
543
544 if (!pszPortName)
545 {
546 return(ERROR_INVALID_PARAMETER);
547 }
548 strcpy(szDefaultPortDrvPath,PATH_CUPS_PDR);
549 DosQuerySysInfo (QSV_BOOT_DRIVE, QSV_BOOT_DRIVE, &ulBootDrive,
550 sizeof (ULONG));
551 szDefaultPortDrvPath[0] = (CHAR)(ulBootDrive + 'A' - 1);
552
553 strcpy( szPathName, szDefaultPortDrvPath );
554
555 /* Make sure the port driver itself is installed */
556 PrfWriteProfileString (HINI_SYSTEMPROFILE,
557 "PM_PORT_DRIVER",
558 "CUPS",
559 szDefaultPortDrvPath);
560
561 /* Generate appname for "PM_CUPSx" */
562 strcpy (chBuf, APPNAME_LEAD_STR);
563 strcat (chBuf, pszPortName);
564
565 /*
566 ** Check for this being our default Port Name to install.
567 ** If so(pszPortName == "CUPS") then generate a unique
568 ** port name so that we can install multiple CUPS printers.
569 */
570 if (!strcmp(pszPortName, DEF_PORTNAME))
571 {
572 /*
573 ** Use chBuf to store the new portname to install
574 ** Must first increment past "PM_" in chBuf
575 */
576 pszPortName = chBuf + 3;
577 GenerateUniquePortName( pszPortName );
578 }
579
580 /* Get port description (fall back to portname if unavailable) */
581 hModule = 0L ; /* Init module handle to null */
582 DosLoadModule (NULL, 0, szPathName, &hModule);
583 if (!GetPortDescription (hab, hModule, pszPortName, chPortDesc))
584 {
585 strcpy( chPortDesc, pszPortName );
586 }
587 DosFreeModule (hModule);
588
589 if (!PrfWriteProfileString (HINI_SYSTEMPROFILE,
590 chBuf,
591 KEY_DESCRIPTION,
592 chPortDesc))
593 {
594 return (WinGetLastError (hab));
595 }
596
597 if (!PrfWriteProfileString (HINI_SYSTEMPROFILE,
598 chBuf,
599 KEY_INITIALIZATION,
600 DEF_INITIALIZATION))
601 {
602 return (WinGetLastError (hab));
603 }
604
605 if (!PrfWriteProfileString (HINI_SYSTEMPROFILE,
606 chBuf,
607 KEY_TERMINATION,
608 DEF_TERMINATION))
609 {
610 return (WinGetLastError (hab));
611 }
612
613 if (!PrfWriteProfileString (HINI_SYSTEMPROFILE,
614 chBuf,
615 KEY_PORTDRIVER,
616 DEF_PORTDRIVER))
617 {
618 return (WinGetLastError (hab));
619 }
620
621 if (!PrfWriteProfileString (HINI_SYSTEMPROFILE,
622 chBuf,
623 KEY_TIMEOUT,
624 DEF_TIMEOUT))
625 {
626 return (WinGetLastError (hab));
627 }
628 if (!PrfWriteProfileString (HINI_SYSTEMPROFILE,
629 APPNAME_PM_SPOOLER_PORT,
630 pszPortName,
631 DEF_TERMINATION))
632 {
633 return (WinGetLastError (hab));
634 }
635 return(NO_ERROR);
636}
637BOOL APIENTRY SplPdGetPortIcon ( HAB hab,
638 PULONG idIcon )
639{
640 if (idIcon)
641 {
642 *idIcon = CUPS_ICON;
643 }
644 return(TRUE);
645}
646APIRET APIENTRY SplPdQueryPort ( HAB hab,
647 PSZ pszPortName,
648 PVOID pBufIn,
649 ULONG cbBuf,
650 PULONG cItems )
651{
652 HMODULE hModule;
653 CHAR chString[STR_LEN_DESC];
654 USHORT usNumLines;
655 USHORT usLineID;
656 USHORT usStrLength;
657 ULONG ulBootDrive;
658 PCH pBuf = pBufIn;
659 CHAR szPathName[260]; /* will contain full path to this port driver */
660
661 if (!cItems)
662 {
663 return(ERROR_INVALID_PARAMETER);
664 }
665
666 if (!pBuf || !cbBuf)
667 {
668 return(ERROR_INVALID_PARAMETER);
669 }
670
671
672 DosQuerySysInfo (QSV_BOOT_DRIVE, QSV_BOOT_DRIVE, &ulBootDrive,
673 sizeof (ULONG));
674 szDefaultPortDrvPath[0] = (CHAR)(ulBootDrive + 'A' - 1);
675
676 PrfQueryProfileString (HINI_SYSTEMPROFILE,
677 "PM_PORT_DRIVER",
678 "CUPS",
679 szDefaultPortDrvPath,
680 szPathName,
681 256 );
682
683 hModule = 0L ;
684
685 DosLoadModule (NULL, 0, szPathName, &hModule);
686
687 chString[0] = '\0' ;
688
689 WinLoadString(hab, hModule, (USHORT)ID_NUMBER_OF_DESC_LINES, STR_LEN_DESC,
690 chString);
691 usNumLines = (USHORT)atoi (chString);
692 usLineID = ID_FIRST_DESC_LINES;
693 for (*cItems = 0; *cItems < usNumLines; *cItems++)
694 {
695 WinLoadString(hab, hModule, usLineID, STR_LEN_DESC, chString);
696 if (cbBuf >= (usStrLength = (USHORT)(strlen (chString) + 1)))
697 {
698 strcpy (pBuf, chString);
699 pBuf += usStrLength;
700 cbBuf -= usStrLength;
701 }
702 else
703 {
704 DosFreeModule (hModule);
705 return(ERROR_INSUFFICIENT_BUFFER);
706 }
707 }
708 DosFreeModule (hModule);
709 return(NO_ERROR);
710}
711APIRET APIENTRY SplPdSetPort ( HAB hab,
712 PSZ pszPortName,
713 PULONG flModified )
714{
715 CHAR chBuf[STR_LEN_PORTNAME];
716 CHAR chPortDriver[STR_LEN_PORTNAME];
717 ULONG ulBootDrive;
718 HMODULE hModule;
719 CHAR szPathName[260]; /* will contain full path to this port driver */
720
721 if (!pszPortName || !flModified)
722 {
723 return(ERROR_INVALID_PARAMETER);
724 }
725
726 strcpy (chBuf, APPNAME_LEAD_STR);
727 strcat (chBuf, pszPortName);
728
729 if (!(PrfQueryProfileString (HINI_SYSTEMPROFILE, chBuf,
730 KEY_PORTDRIVER, NULL, chPortDriver,
731 STR_LEN_PORTNAME)))
732 {
733 return(ERROR_INVALID_PARAMETER);
734 }
735
736 if (strcmp (chPortDriver, DEF_PORTDRIVER))
737 {
738 return(ERROR_INVALID_PARAMETER);
739 }
740
741 DosQuerySysInfo (QSV_BOOT_DRIVE, QSV_BOOT_DRIVE, &ulBootDrive,
742 sizeof (ULONG));
743 szDefaultPortDrvPath[0] = (CHAR)(ulBootDrive + 'A' - 1);
744
745 PrfQueryProfileString (HINI_SYSTEMPROFILE,
746 "PM_PORT_DRIVER",
747 "CUPS",
748 szDefaultPortDrvPath,
749 szPathName,
750 256 );
751
752 hModule = 0L ; /* Init module handle to null */
753
754 DosLoadModule (NULL, 0, szPathName, &hModule);
755
756 *flModified = OpenLprPortDlg (hab, hModule, pszPortName, chBuf);
757
758 DosFreeModule (hModule);
759 return(NO_ERROR);
760}
761
762
763APIRET APIENTRY SplPdRemovePort ( HAB hab,
764 PSZ pszPortName )
765{
766 CHAR chBuf[STR_LEN_PORTNAME];
767 CHAR chPortDriver[STR_LEN_PORTNAME];
768
769 if (!pszPortName)
770 {
771 return(ERROR_INVALID_PARAMETER);
772 }
773
774 strcpy (chBuf, APPNAME_LEAD_STR);
775 strcat (chBuf, pszPortName);
776
777 if (!(PrfQueryProfileString (HINI_SYSTEMPROFILE, chBuf,
778 KEY_PORTDRIVER, NULL, chPortDriver,
779 STR_LEN_PORTNAME)))
780 {
781 return(ERROR_INVALID_PARAMETER);
782 }
783
784 if (strcmp (chPortDriver, DEF_PORTDRIVER))
785 {
786 return(ERROR_INVALID_PARAMETER);
787 }
788
789 PrfWriteProfileString (HINI_SYSTEMPROFILE, chBuf, NULL, NULL);
790
791 PrfWriteProfileString (HINI_SYSTEMPROFILE,
792 APPNAME_PM_SPOOLER_PORT,
793 pszPortName,
794 NULL);
795 return(NO_ERROR);
796
797}
798ULONG APIENTRY SplPdOpen( PSZ pszPortName,
799 PHFILE phFile,
800 PULONG pDeviceFlags,
801 PVOID pPrtOpenStruct)
802{
803 APIRET rc;
804 ULONG ulAction = 0; /* Action taken by DosOpen */
805 ULONG ulBytesRead = 0; /* Number of bytes read by DosRead */
806 ULONG ulWrote = 0; /* Number of bytes written by DosWrite */
807 ULONG ulLocal = 0; /* File pointer position after DosSetFilePtr */
808 CHAR szTemp[ 256];
809 UCHAR tmp[256];
810 ULONG pcbWritten ;
811 USHORT i;
812 HFILE control;
813 UCHAR filename[256];
814 DATETIME dt;
815 UCHAR spool_dir[256];
816 PEAOP2 pEABuf = NULL;
817 UCHAR pszPSHeader[] = "%!PS-Adobe-3.0\n";
818 ULONG cbHeader;
819
820
821 if (!phFile || !pDeviceFlags )
822 {
823 return(ERROR_INVALID_PARAMETER);
824 }
825 DosGetDateTime(&dt);
826 rc = PrfQueryProfileString (HINI_SYSTEMPROFILE,
827 "PM_SPOOLER",
828 "DIR",
829 NULL,
830 (PSZ)spool_dir,
831 sizeof(spool_dir));
832 spool_dir[ strlen(spool_dir) - 1] = '\0';
833 sprintf(tmp,"%s\\CUPS",spool_dir);
834 DosCreateDir( tmp,pEABuf );
835 sprintf(filename,"%s\\CUPS\\%02d_%02d_%02d_%02d_%s",spool_dir,dt.hours,dt.minutes,dt.seconds,dt.hundredths,pszPortName);
836 rc = DosOpen(filename,
837 phFile, /* File handle */
838 &ulAction, /* Action taken */
839 100L, /* File primary allocation */
840 FILE_ARCHIVED | FILE_NORMAL, /* File attribute */
841 OPEN_ACTION_CREATE_IF_NEW |
842 OPEN_ACTION_OPEN_IF_EXISTS, /* Open function type */
843 OPEN_FLAGS_NOINHERIT |
844 OPEN_SHARE_DENYNONE |
845 OPEN_ACCESS_READWRITE, /* Open mode of the file */
846 0L); /* No extended attribute */
847 DosWrite(*phFile,pszPSHeader,strlen(pszPSHeader),&cbHeader);
848 sprintf(szTemp,"PM_%s",pszPortName);
849 if (PrfQueryProfileString (HINI_SYSTEMPROFILE,
850 szTemp,
851 KEY_INITIALIZATION,
852 NULL,
853 szTemp,
854 sizeof(szTemp)))
855 {
856 sprintf(tmp ,"%s\\CUPS\\%d.control",spool_dir,*phFile);
857 rc = DosOpen( tmp ,
858 &control, /* File handle */
859 &ulAction, /* Action taken */
860 strlen(szTemp), /* File primary allocation */
861 FILE_ARCHIVED | FILE_NORMAL, /* File attribute */
862 OPEN_ACTION_CREATE_IF_NEW |
863 OPEN_ACTION_OPEN_IF_EXISTS, /* Open function type */
864 OPEN_FLAGS_NOINHERIT |
865 OPEN_SHARE_DENYNONE |
866 OPEN_ACCESS_READWRITE, /* Open mode of the file */
867 0L); /* No extended attribute */
868 rc = DosWrite( control,szTemp,strlen(szTemp),&pcbWritten);
869 rc = DosWrite( control,"#",1,&pcbWritten);
870 rc = DosWrite( control,filename,strlen(filename),&pcbWritten);
871 rc = DosWrite( control,"@",1,&pcbWritten);
872 rc = DosClose(control);
873 }
874 return rc;
875
876}
877ULONG APIENTRY SplPdQuery ( PSZ pszPortName,
878 ULONG ulFlags,
879 ULONG ulCommand,
880 PVOID pInData,
881 ULONG cbInData,
882 PVOID pOutData,
883 PULONG pcbOutData )
884{
885 PPORTSETTINGS pSettings;
886 CHAR szIniStr[ STR_LEN_PORTDESC ];
887 CHAR szAppName[ STR_LEN_PORTNAME ];
888 PUCHAR token;
889 ULONG rc;
890
891 // Generate appname for "PM_CUPSx"
892 strcpy( szAppName, APPNAME_LEAD_STR);
893 strncat( szAppName, pszPortName, STR_LEN_PORTNAME-1 );
894
895 switch ( ulCommand ) {
896
897 case BIDI_Q_PORTDRV:
898 // Make sure the data is valid
899 if ( !pOutData || *pcbOutData < sizeof( PORTSETTINGS ))
900 return( ERROR_INVALID_PARAMETER );
901 pSettings = (PPORTSETTINGS) pOutData;
902
903 if ( PrfQueryProfileString( HINI_SYSTEMPROFILE, szAppName,
904 KEY_INITIALIZATION, NULL,
905 szIniStr, STR_LEN_PORTDESC )) {
906 if ( !( token = lprtok( szIniStr, "#")))
907 pSettings->szHost[ 0 ] = '\0';
908 else
909 strncpy( pSettings->szHost, token, STR_LEN_HOSTNAME );
910 if ( !( token = lprtok( NULL, "#")))
911 pSettings->szQueue[ 0 ] = '\0';
912 else {
913 if ( token[ strlen(token) - 1 ] == ';')
914 token[ strlen(token) - 1 ] = '\0';
915 strncpy( pSettings->szHost, token, STR_LEN_QUEUENAME );
916 }
917 }
918 *pcbOutData = sizeof( PORTSETTINGS );
919 rc = NO_ERROR;
920 break;
921
922 default:
923 rc = ERROR_NOT_SUPPORTED;
924 //rc = NO_ERROR;
925 break;
926 }
927
928 return ( rc );
929}
930ULONG APIENTRY SplPdSet ( PSZ pszPortName,
931 ULONG ulFlags,
932 ULONG ulCommand,
933 PVOID pInData,
934 ULONG cbInData )
935{
936 PPORTSETTINGS pSettings;
937 CHAR szIniStr[ STR_LEN_PORTDESC ];
938 CHAR szAppName[ STR_LEN_PORTNAME ];
939 ULONG rc;
940
941 // Generate appname for "PM_CUPSx"
942 strcpy( szAppName, APPNAME_LEAD_STR);
943 strncat( szAppName, pszPortName, STR_LEN_PORTNAME-1 );
944
945 switch ( ulCommand ) {
946
947 case BIDI_SET_PORTDRV:
948 // Make sure the data is valid
949 if ( !pInData || cbInData < sizeof( PORTSETTINGS ))
950 return( ERROR_INVALID_PARAMETER );
951 pSettings = (PPORTSETTINGS) pInData;
952 // Update the INITIALIZATION string
953 strncpy( szIniStr, pSettings->szHost, STR_LEN_HOSTNAME );
954 strcat( szIniStr, "#");
955 strncat( szIniStr, pSettings->szQueue, STR_LEN_PORTDESC-1 );
956 PrfWriteProfileString( HINI_SYSTEMPROFILE, szAppName,
957 KEY_INITIALIZATION, szIniStr );
958 PrfWriteProfileString( HINI_SYSTEMPROFILE, APPNAME_PM_SPOOLER_PORT,
959 pszPortName, szIniStr );
960 // Update the DESCRIPTION string
961 strncpy( szIniStr, pSettings->szHost, STR_LEN_HOSTNAME );
962 strcat( szIniStr, ":");
963 strncat( szIniStr, pSettings->szQueue, STR_LEN_PORTDESC-1 );
964 PrfWriteProfileString( HINI_SYSTEMPROFILE,
965 szAppName, KEY_DESCRIPTION, szIniStr );
966/*
967 * // Update the TIMEOUT string
968 * sprintf( szIniStr, "%u;", pSettings->ulTimeOut );
969 * PrfWriteProfileString( HINI_SYSTEMPROFILE,
970 * szAppName, KEY_TIMEOUT, szIniStr );
971 */
972 rc = NO_ERROR;
973 break;
974
975 default:
976 //rc = ERROR_NOT_SUPPORTED;
977 rc = NO_ERROR;
978 break;
979 }
980 return ( rc );
981
982}
983ULONG APIENTRY SplPdNewPage ( HFILE hFile, ULONG ulPageNumber )
984{
985 return NO_ERROR;
986}
987ULONG APIENTRY SplPdAbortDoc ( HFILE hFile,
988 PVOID pchData,
989 ULONG cbData,
990 ULONG ulFlags )
991{
992 return NO_ERROR;
993}
994ULONG APIENTRY SplPdClose ( HFILE hFile )
995{
996 APIRET rc;
997 APIRET resp;
998 USHORT i;
999 USHORT j;
1000 RESULTCODES rc_child;
1001 ULONG nbr_lu;
1002 ULONG ulAction;
1003 UCHAR szTemp[256];
1004 HFILE control;
1005 UCHAR arg[256];
1006 UCHAR filename[256];
1007 UCHAR ip_add[256];
1008 UCHAR queue_name[256];
1009 USHORT pos;
1010 UCHAR spool_dir[256];
1011 UCHAR errorstr[256];
1012 ULONG ulBootDrive;
1013 UCHAR szExecPath[ CCHMAXPATH ];
1014 CHAR *c;
1015
1016 rc = PrfQueryProfileString (HINI_SYSTEMPROFILE,
1017 "PM_SPOOLER",
1018 "DIR",
1019 NULL,
1020 (PSZ)spool_dir,
1021 sizeof(spool_dir));
1022 spool_dir[ strlen(spool_dir) - 1] = '\0';
1023 sprintf(szTemp,"%s\\CUPS\\%d.control",spool_dir,hFile);
1024 rc = DosOpen(szTemp,
1025 &control,
1026 &ulAction,
1027 0L,
1028 FILE_ARCHIVED | FILE_NORMAL,
1029 OPEN_ACTION_CREATE_IF_NEW |
1030 OPEN_ACTION_OPEN_IF_EXISTS,
1031 OPEN_FLAGS_NOINHERIT |
1032 OPEN_SHARE_DENYNONE |
1033 OPEN_ACCESS_READWRITE,
1034 0L);
1035 rc = DosRead( control,szTemp,sizeof(szTemp),&nbr_lu);
1036 rc = DosClose( control );
1037 sprintf(filename,"%s\\CUPS\\%d.control",spool_dir,hFile);
1038 DosDelete(filename);
1039
1040 i = 0;
1041 j = 0;
1042 pos = 0;
1043 while (szTemp[i] != '@')
1044 {
1045 if (szTemp[i] == '#')
1046 {
1047 szTemp[i] = '\0';
1048 switch(j)
1049 {
1050 case 0:strcpy(ip_add,&szTemp[pos]);
1051 break;
1052 case 1:strcpy(queue_name,&szTemp[pos]);
1053 break;
1054 }
1055 pos = i+1;
1056 j++;
1057 }
1058 i++;
1059 }
1060 szTemp[i] = '\0';
1061 strcpy(filename,&szTemp[pos]);
1062
1063 rc = DosClose( hFile );
1064
1065 /* Locate the TCP/IP BIN directory (it isn't necessarily on the boot drive,
1066 * but we'll fall back to that if all else fails)... unfortunately there's
1067 * no standard way to locate it, so we simply search the PATH for inetd.exe.
1068 */
1069 DosQuerySysInfo(QSV_BOOT_DRIVE, QSV_BOOT_DRIVE, &ulBootDrive, sizeof(ULONG));
1070 if ( !DosSearchPath( SEARCH_IGNORENETERRS |
1071 SEARCH_ENVIRONMENT |
1072 SEARCH_CUR_DIRECTORY,
1073 "PATH", "inetd.exe", szExecPath, CCHMAXPATH ) &&
1074 (( c = strrchr( szExecPath, '\\')) != NULL ))
1075 *c = '\0';
1076 else
1077 sprintf( szExecPath, "%c:\\tcpip\\bin", ulBootDrive+'A'-1 );
1078
1079#if 0
1080 /* Experimental: call user script to fix any remaining CUPS problems in the
1081 * PS data file which aren't handled by the PostScript driver
1082 */
1083 sprintf(arg,"cmd.exe /c %s\\psfixup.cmd %s",szExecPath,filename);
1084 arg[7] = '\0';
1085 rc = DosExecPgm(NULL,0,EXEC_SYNC,arg,NULL,&rc_child,"cmd.exe");
1086#endif
1087
1088 sprintf(arg,"%s\\cupslpr -H %s -P %s %s",szExecPath,ip_add,queue_name,filename);
1089 arg[ strlen(szExecPath) + 8 ] = '\0';
1090 rc = DosExecPgm(NULL,0,EXEC_SYNC,arg,NULL,&rc_child,"cupslpr.exe");
1091 if (rc)
1092 {
1093 sprintf(errorstr,"Error during spooling to CUPS Server");
1094 resp = WinMessageBox (HWND_DESKTOP,
1095 HWND_DESKTOP,
1096 errorstr,
1097 "eCups Port driver error",
1098 0L, MB_RETRYCANCEL | MB_WARNING | MB_MOVEABLE);
1099 if (resp != MBID_CANCEL )
1100 {
1101 rc = DosExecPgm(NULL,0,EXEC_SYNC,arg,NULL,&rc_child,"cupslpr.exe");
1102 }
1103 else rc = 0;
1104 };
1105 strcpy(filename,&szTemp[pos]);
1106 DosDelete(filename);
1107 return rc;
1108}
1109ULONG APIENTRY SplPdWrite( HFILE hFile,
1110 PVOID pchData,
1111 ULONG cbData,
1112 PULONG pcbWritten )
1113{ APIRET rc;
1114
1115 rc = DosWrite( hFile,pchData,cbData,pcbWritten);
1116 rc = DosSleep(0);
1117 return rc;
1118}
1119
Note: See TracBrowser for help on using the repository browser.