source: branches/1.0/src/splpd.c@ 31

Last change on this file since 31 was 31, checked in by herwigb, 15 years ago

Streamline code

File size: 15.4 KB
Line 
1
2#include "uni.h"
3
4#include <stdlib.h>
5#include <string.h>
6#include <ctype.h>
7#include <stdarg.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <process.h>
11#include "utils.h"
12
13//
14// If port driver is not defined in INI file yet
15// assume it exists in the boot drive's \OS2\DLL directory
16//
17
18CHAR szDefaultPortDrvPath[] = { PATH_UNI_PDR };
19
20
21APIRET APIENTRY SplPdEnumPort ( HAB hab,
22 PVOID pBuf,
23 ULONG cbBuf,
24 PULONG pulReturned,
25 PULONG pulTotal,
26 PULONG pcbRequired )
27
28{
29 HMODULE hModule;
30 ULONG ulBootDrive;
31 ULONG rcLoadMod;
32 CHAR szPathName[260];
33
34 if (!pulReturned ||
35 !pulTotal ||
36 !pcbRequired)
37 {
38 return(ERROR_INVALID_PARAMETER);
39 }
40
41 if (!pBuf && cbBuf)
42 {
43 return(ERROR_INVALID_PARAMETER);
44 }
45
46 DosQuerySysInfo (QSV_BOOT_DRIVE, QSV_BOOT_DRIVE, &ulBootDrive,
47 sizeof (ULONG));
48 szDefaultPortDrvPath[0] = (CHAR)(ulBootDrive + 'A' - 1);
49
50 PrfQueryProfileString (HINI_SYSTEMPROFILE,
51 "PM_PORT_DRIVER",
52 "UNI",
53 szDefaultPortDrvPath,
54 szPathName,
55 256 );
56
57 rcLoadMod = DosLoadModule (NULL, 0, szPathName, &hModule);
58
59 if (cbBuf == 0L)
60 {
61 *pulReturned = 0;
62 *pcbRequired = CalcBufLength (hab, hModule);
63 *pulTotal = MAX_PORTS;
64 if (!rcLoadMod)
65 DosFreeModule (hModule);
66 return(ERROR_MORE_DATA);
67 }
68
69 /*
70 ** check number of ports info we can fit in supplied buffer
71 */
72 *pulTotal = MAX_PORTS;
73 *pcbRequired = CalcBufLength (hab, hModule);
74 *pulReturned = NumPortsCanFit (hab, hModule, cbBuf);
75
76 /*
77 ** return error if we can not fit one port.
78 */
79 if (!(*pulReturned))
80 {
81 if (!rcLoadMod)
82 DosFreeModule (hModule);
83 return(ERROR_INSUFFICIENT_BUFFER);
84 }
85
86 /*
87 ** copy all the ports which we can store in the pBuf
88 */
89 CopyNPorts (hab, hModule, (PCH)pBuf, *pulReturned);
90
91 /*
92 ** Free the module - we do not need it any more.
93 */
94 if (!rcLoadMod)
95 DosFreeModule (hModule);
96
97 /*
98 ** copy all the ports which we can store in the pBuf
99 */
100 if (*pulReturned < *pulTotal)
101 {
102 return(ERROR_MORE_DATA);
103 }
104
105 return(NO_ERROR);
106}
107APIRET APIENTRY SplPdInstallPort ( HAB hab,
108 PSZ pszPortName )
109{
110 CHAR chBuf[STR_LEN_PORTNAME];
111 CHAR chPortDesc[STR_LEN_PORTDESC];
112 ULONG ulBootDrive;
113 HMODULE hModule;
114 CHAR szPathName[260]; /* will contain full path to this port driver */
115
116 if (!pszPortName)
117 {
118 return(ERROR_INVALID_PARAMETER);
119 }
120 strcpy(szDefaultPortDrvPath,PATH_UNI_PDR);
121 DosQuerySysInfo (QSV_BOOT_DRIVE, QSV_BOOT_DRIVE, &ulBootDrive,
122 sizeof (ULONG));
123 szDefaultPortDrvPath[0] = (CHAR)(ulBootDrive + 'A' - 1);
124 strcpy( szPathName, szDefaultPortDrvPath );
125
126 /* Make sure the port driver itself is installed */
127 PrfWriteProfileString (HINI_SYSTEMPROFILE,
128 "PM_PORT_DRIVER",
129 "UNI",
130 szDefaultPortDrvPath);
131
132 /* Generate appname for "PM_UNIx" */
133 strcpy (chBuf, APPNAME_LEAD_STR);
134 strcat (chBuf, pszPortName);
135
136 /*
137 ** Check for this being our default Port Name to install.
138 ** If so (pszPortName == "UNI") then generate a unique
139 ** port name so that we can install multiple UNI printers.
140 */
141 if (!strcmp(pszPortName, DEF_PORTNAME))
142 {
143 /*
144 ** Use chBuf to store the new portname to install
145 ** Must first increment past "PM_" in chBuf
146 */
147 pszPortName = chBuf + 3;
148 GenerateUniquePortName( pszPortName );
149 }
150
151 /* Get initial port description (fall back to portname if unavailable) */
152 hModule = 0L ; /* Init module handle to null */
153 DosLoadModule (NULL, 0, szPathName, &hModule);
154 if (!GetPortDescription (hab, hModule, pszPortName, chPortDesc))
155 {
156 strcpy( chPortDesc, pszPortName );
157 }
158 DosFreeModule (hModule);
159
160 if (!PrfWriteProfileString (HINI_SYSTEMPROFILE,
161 chBuf,
162 KEY_DESCRIPTION,
163 chPortDesc))
164 {
165 return (WinGetLastError (hab));
166 }
167
168 if (!PrfWriteProfileString (HINI_SYSTEMPROFILE,
169 chBuf,
170 KEY_INITIALIZATION,
171 DEF_INITIALIZATION))
172 {
173 return (WinGetLastError (hab));
174 }
175
176 if (!PrfWriteProfileString (HINI_SYSTEMPROFILE,
177 chBuf,
178 KEY_TERMINATION,
179 DEF_TERMINATION))
180 {
181 return (WinGetLastError (hab));
182 }
183
184 if (!PrfWriteProfileString (HINI_SYSTEMPROFILE,
185 chBuf,
186 KEY_PORTDRIVER,
187 DEF_PORTDRIVER))
188 {
189 return (WinGetLastError (hab));
190 }
191
192 if (!PrfWriteProfileString (HINI_SYSTEMPROFILE,
193 chBuf,
194 KEY_TIMEOUT,
195 DEF_TIMEOUT))
196 {
197 return (WinGetLastError (hab));
198 }
199 if (!PrfWriteProfileString (HINI_SYSTEMPROFILE,
200 APPNAME_PM_SPOOLER_PORT,
201 pszPortName,
202 DEF_INITIALIZATION))
203 {
204 return (WinGetLastError (hab));
205 }
206 return(NO_ERROR);
207}
208BOOL APIENTRY SplPdGetPortIcon ( HAB hab,
209 PULONG idIcon )
210{
211 if (idIcon)
212 {
213 *idIcon = UNI_ICON;
214 }
215 return(TRUE);
216}
217APIRET APIENTRY SplPdQueryPort ( HAB hab,
218 PSZ pszPortName,
219 PVOID pBufIn,
220 ULONG cbBuf,
221 PULONG cItems )
222{
223 HMODULE hModule;
224 CHAR chString[STR_LEN_DESC];
225 USHORT usNumLines;
226 USHORT usLineID;
227 USHORT usStrLength;
228 ULONG ulBootDrive;
229 PCH pBuf = pBufIn;
230 CHAR szPathName[260]; /* will contain full path to this port driver */
231
232 if (!cItems)
233 {
234 return(ERROR_INVALID_PARAMETER);
235 }
236
237 if (!pBuf || !cbBuf)
238 {
239 return(ERROR_INVALID_PARAMETER);
240 }
241
242
243 DosQuerySysInfo (QSV_BOOT_DRIVE, QSV_BOOT_DRIVE, &ulBootDrive,
244 sizeof (ULONG));
245 szDefaultPortDrvPath[0] = (CHAR)(ulBootDrive + 'A' - 1);
246
247 PrfQueryProfileString (HINI_SYSTEMPROFILE,
248 "PM_PORT_DRIVER",
249 "UNI",
250 szDefaultPortDrvPath,
251 szPathName,
252 256 );
253
254 hModule = 0L ;
255
256 DosLoadModule (NULL, 0, szPathName, &hModule);
257
258 chString[0] = '\0' ;
259
260 WinLoadString(hab, hModule, (USHORT)ID_NUMBER_OF_DESC_LINES, STR_LEN_DESC,
261 chString);
262 usNumLines = (USHORT)atoi (chString);
263 usLineID = ID_FIRST_DESC_LINES;
264 for (*cItems = 0; *cItems < usNumLines; *cItems++)
265 {
266 WinLoadString(hab, hModule, usLineID, STR_LEN_DESC, chString);
267 if (cbBuf >= (usStrLength = (USHORT)(strlen (chString) + 1)))
268 {
269 strcpy (pBuf, chString);
270 pBuf += usStrLength;
271 cbBuf -= usStrLength;
272 }
273 else
274 {
275 DosFreeModule (hModule);
276 return(ERROR_INSUFFICIENT_BUFFER);
277 }
278 }
279 DosFreeModule (hModule);
280 return(NO_ERROR);
281}
282APIRET APIENTRY SplPdSetPort ( HAB hab,
283 PSZ pszPortName,
284 PULONG flModified )
285{
286 CHAR chBuf[STR_LEN_PORTNAME];
287 CHAR chPortDriver[STR_LEN_PORTNAME];
288 ULONG ulBootDrive;
289 HMODULE hModule;
290 CHAR szPathName[260]; /* will contain full path to this port driver */
291
292 if (!pszPortName || !flModified)
293 {
294 return(ERROR_INVALID_PARAMETER);
295 }
296
297 strcpy (chBuf, APPNAME_LEAD_STR);
298 strcat (chBuf, pszPortName);
299
300 if (!(PrfQueryProfileString (HINI_SYSTEMPROFILE, chBuf,
301 KEY_PORTDRIVER, NULL, chPortDriver,
302 STR_LEN_PORTNAME)))
303 {
304 return(ERROR_INVALID_PARAMETER);
305 }
306
307 if (strcmp (chPortDriver, DEF_PORTDRIVER))
308 {
309 return(ERROR_INVALID_PARAMETER);
310 }
311
312 DosQuerySysInfo (QSV_BOOT_DRIVE, QSV_BOOT_DRIVE, &ulBootDrive,
313 sizeof (ULONG));
314 szDefaultPortDrvPath[0] = (CHAR)(ulBootDrive + 'A' - 1);
315
316 PrfQueryProfileString (HINI_SYSTEMPROFILE,
317 "PM_PORT_DRIVER",
318 "UNI",
319 szDefaultPortDrvPath,
320 szPathName,
321 256 );
322
323 hModule = 0L ; /* Init module handle to null */
324
325 DosLoadModule (NULL, 0, szPathName, &hModule);
326
327 *flModified = OpenUniPortDlg (hab, hModule, pszPortName, chBuf);
328
329 DosFreeModule (hModule);
330 return(NO_ERROR);
331}
332
333
334APIRET APIENTRY SplPdRemovePort ( HAB hab,
335 PSZ pszPortName )
336{
337 CHAR chBuf[STR_LEN_PORTNAME];
338 CHAR chPortDriver[STR_LEN_PORTNAME];
339
340 if (!pszPortName)
341 {
342 return(ERROR_INVALID_PARAMETER);
343 }
344
345 strcpy (chBuf, APPNAME_LEAD_STR);
346 strcat (chBuf, pszPortName);
347
348 if (!(PrfQueryProfileString (HINI_SYSTEMPROFILE, chBuf,
349 KEY_PORTDRIVER, NULL, chPortDriver,
350 STR_LEN_PORTNAME)))
351 {
352 return(ERROR_INVALID_PARAMETER);
353 }
354
355 if (strcmp (chPortDriver, DEF_PORTDRIVER))
356 {
357 return(ERROR_INVALID_PARAMETER);
358 }
359
360 PrfWriteProfileString (HINI_SYSTEMPROFILE, chBuf, NULL, NULL);
361
362 PrfWriteProfileString (HINI_SYSTEMPROFILE,
363 APPNAME_PM_SPOOLER_PORT,
364 pszPortName,
365 NULL);
366 return(NO_ERROR);
367
368}
369ULONG APIENTRY SplPdOpen( PSZ pszPortName,
370 PHFILE phFile,
371 PULONG pDeviceFlags,
372 PVOID pPrtOpenStruct)
373{
374 APIRET rc;
375 ULONG ulAction = 0; /* Action taken by DosOpen */
376 ULONG ulBytesRead = 0; /* Number of bytes read by DosRead */
377 ULONG ulWrote = 0; /* Number of bytes written by DosWrite */
378 ULONG ulLocal = 0; /* File pointer position after DosSetFilePtr */
379 char szTemp[ 256];
380 char tmp[256];
381 ULONG pcbWritten ;
382 USHORT i;
383 HFILE control;
384 char filename[256];
385 DATETIME dt;
386 char spool_dir[256];
387 PEAOP2 pEABuf = NULL;
388
389
390 if (!phFile || !pDeviceFlags )
391 {
392 return(ERROR_INVALID_PARAMETER);
393 }
394 DosGetDateTime(&dt);
395 rc = PrfQueryProfileString (HINI_SYSTEMPROFILE,
396 "PM_SPOOLER",
397 "DIR",
398 NULL,
399 (PSZ)spool_dir,
400 sizeof(spool_dir));
401 spool_dir[ strlen(spool_dir) - 1] = '\0';
402 sprintf(tmp,"%s\\UNI",spool_dir);
403 DosCreateDir( tmp,pEABuf );
404 sprintf(filename,"%s\\UNI\\%02d_%02d_%02d_%02d_%s",spool_dir,dt.hours,dt.minutes,dt.seconds,dt.hundredths,pszPortName);
405 rc = DosOpen(filename,
406 phFile, /* File handle */
407 &ulAction, /* Action taken */
408 100L, /* File primary allocation */
409 FILE_ARCHIVED | FILE_NORMAL, /* File attribute */
410 OPEN_ACTION_CREATE_IF_NEW |
411 OPEN_ACTION_OPEN_IF_EXISTS, /* Open function type */
412 OPEN_FLAGS_NOINHERIT |
413 OPEN_SHARE_DENYNONE |
414 OPEN_ACCESS_READWRITE, /* Open mode of the file */
415 0L); /* No extended attribute */
416/* DosWrite(*phFile,pszPSHeader,strlen(pszPSHeader),&cbHeader); */
417 sprintf(szTemp,"PM_%s",pszPortName);
418 if (PrfQueryProfileString (HINI_SYSTEMPROFILE,
419 szTemp,
420 KEY_INITIALIZATION,
421 NULL,
422 szTemp,
423 sizeof(szTemp)))
424 {
425 sprintf(tmp ,"%s\\UNI\\%d.control",spool_dir,*phFile);
426 rc = DosOpen( tmp ,
427 &control, /* File handle */
428 &ulAction, /* Action taken */
429 strlen(szTemp), /* File primary allocation */
430 FILE_ARCHIVED | FILE_NORMAL, /* File attribute */
431 OPEN_ACTION_CREATE_IF_NEW |
432 OPEN_ACTION_OPEN_IF_EXISTS, /* Open function type */
433 OPEN_FLAGS_NOINHERIT |
434 OPEN_SHARE_DENYNONE |
435 OPEN_ACCESS_READWRITE, /* Open mode of the file */
436 0L); /* No extended attribute */
437 rc = DosWrite( control,szTemp,strlen(szTemp),&pcbWritten);
438 rc = DosWrite( control,"#",1,&pcbWritten);
439 rc = DosWrite( control,filename,strlen(filename),&pcbWritten);
440 rc = DosWrite( control,"@",1,&pcbWritten);
441 rc = DosClose(control);
442 }
443return rc;
444
445}
446ULONG APIENTRY SplPdQuery ( PSZ pszDeviceName,
447 ULONG ulFlags,
448 ULONG ulCommand,
449 PVOID pInData,
450 ULONG cbInData,
451 PVOID pOutData,
452 PULONG pcbOutData )
453{
454/* return ERROR_NOT_SUPPORTED; */
455 return NO_ERROR;
456}
457ULONG APIENTRY SplPdSet ( PSZ pszDeviceName,
458 ULONG ulFlags,
459 ULONG ulCommand,
460 PVOID pInData,
461 ULONG cbInData )
462{
463/* return ERROR_NOT_SUPPORTED; */
464 return NO_ERROR;
465}
466ULONG APIENTRY SplPdNewPage ( HFILE hFile, ULONG ulPageNumber )
467{
468 return NO_ERROR;
469}
470ULONG APIENTRY SplPdAbortDoc( HFILE hFile,
471 PVOID pchData,
472 ULONG cbData,
473 ULONG ulFlags )
474{
475 return NO_ERROR;
476}
477ULONG APIENTRY SplPdClose( HFILE hFile )
478{
479 APIRET rc;
480 APIRET resp;
481 USHORT i;
482 USHORT j;
483 RESULTCODES rc_child;
484 ULONG nbr_lu;
485 ULONG ulAction;
486 char szTemp[256];
487 HFILE control;
488 char binfile[256];
489 char arg[256];
490 char *j_parms;
491 int allocSize = 0;
492 char j_id[3];
493 char parameters[256];
494 char workingdir[256] ;
495 char j_title[256];
496 char j_copies[3];
497 char j_options[8];
498 char filename[256];
499 char ip_add[256];
500 char queue_name[256];
501 char workgroup[256];
502 char username[256];
503 char password_enc[256];
504 char password_dec[256];
505 char errorstr[256];
506 USHORT pos;
507 char spool_dir[256];
508 ULONG ulBootDrive;
509 char *p_arg[10];
510
511 rc = PrfQueryProfileString (HINI_SYSTEMPROFILE,
512 "PM_SPOOLER",
513 "DIR",
514 NULL,
515 (PSZ)spool_dir,
516 sizeof(spool_dir));
517 spool_dir[ strlen(spool_dir) - 1] = '\0';
518 sprintf(szTemp,"%s\\UNI\\%d.control",spool_dir,hFile);
519 rc = DosOpen(szTemp,
520 &control,
521 &ulAction,
522 0L,
523 FILE_ARCHIVED | FILE_NORMAL,
524 OPEN_ACTION_CREATE_IF_NEW |
525 OPEN_ACTION_OPEN_IF_EXISTS,
526 OPEN_FLAGS_NOINHERIT |
527 OPEN_SHARE_DENYNONE |
528 OPEN_ACCESS_READWRITE,
529 0L);
530 rc = DosRead( control,szTemp,sizeof(szTemp),&nbr_lu);
531 rc = DosClose( control );
532 sprintf(filename,"%s\\UNI\\%d.control",spool_dir,hFile);
533 DosDelete(filename);
534
535 i = 0;
536 j = 0;
537 pos = 0;
538 while (szTemp[i] != '@')
539 {
540 if (szTemp[i] == '#')
541 {
542 szTemp[i] = '\0';
543 switch(j)
544 {
545 case 0:strcpy(binfile,&szTemp[pos]);
546 break;
547 case 1:strcpy(parameters,&szTemp[pos]);
548 break;
549 case 2:strcpy(workingdir,&szTemp[pos]);
550 break;
551/* case 3:strcpy(username,&szTemp[pos]);
552 break;
553 case 4:strcpy(j_copies,&szTemp[pos]);
554 break;
555 case 5:strcpy(password_enc,&szTemp[pos]);
556 break; */
557 }
558 pos = i+1;
559 j++;
560 }
561 i++;
562 }
563 szTemp[i] = '\0';
564 strcpy(filename,&szTemp[pos]);
565
566 rc = DosClose( hFile );
567 DosQuerySysInfo (QSV_BOOT_DRIVE, QSV_BOOT_DRIVE, &ulBootDrive,
568 sizeof (ULONG));
569
570// decryptPassword(password_enc,password_dec);
571
572// allocate needed space for the returning string +1 (for string terminator)
573 allocSize = strlen(parameters) - strlen("%file%") + strlen(filename);
574
575 j_parms = malloc((allocSize +1) * sizeof(char));
576
577// searchReplace(search, replace, string, replaced, allocSize);
578 searchReplace("%file%", filename, parameters, j_parms, allocSize);
579
580// split into single paramters
581 p_arg[0] = binfile;
582 for (i=1; i < 10; i++) {
583 if ( i == 1) {
584 p_arg[i] = strtok(j_parms, " ");
585 } else {
586 p_arg[i] = strtok(NULL, " ");
587 }
588 };
589 p_arg[10] = NULL;
590
591 if (strlen(workingdir) > 0)
592 {
593 chdir(workingdir);
594 };
595
596 rc = spawnvp(P_WAIT,p_arg[0],p_arg);
597
598 while (rc != 0)
599 {
600 sprintf(errorstr,"Error during spooling to %s %s ",binfile,j_parms,NULL);
601
602 resp = WinMessageBox (HWND_DESKTOP,
603 HWND_DESKTOP,
604 errorstr,
605 "Universal Port driver error",
606 0L, MB_RETRYCANCEL | MB_WARNING | MB_MOVEABLE);
607 if (resp != MBID_CANCEL )
608 {
609 rc = spawnvp(P_WAIT,p_arg[0],p_arg);
610 }
611 else rc = 0;
612 };
613
614 free(j_parms);
615
616 strcpy(filename,&szTemp[pos]);
617 DosDelete(filename);
618
619 /* We always have to return success to the spooler subsystem */
620 rc = NO_ERROR;
621 return rc;
622}
623ULONG APIENTRY SplPdWrite( HFILE hFile,
624 PVOID pchData,
625 ULONG cbData,
626 PULONG pcbWritten )
627{ APIRET rc;
628
629 rc = DosWrite( hFile,pchData,cbData,pcbWritten);
630 rc = DosSleep(0);
631 return rc;
632}
Note: See TracBrowser for help on using the repository browser.