source: trunk/gui/util/prntobj/prntobj.c@ 50

Last change on this file since 50 was 29, checked in by Alex Taylor, 12 years ago

Add more informative output.

File size: 5.6 KB
Line 
1/* PRNTOBJ
2 *
3 * Simple program to create a new printer object
4 *
5 * Syntax: prntobj <queuename> <portname> <driver> <title>
6 */
7
8#define INCL_DOSERRORS
9#define INCL_SPL
10#define INCL_SPLDOSPRINT
11#define INCL_SPLERRORS
12#include <os2.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16
17
18#define MAX_DEVICE_NAME 9
19
20
21/* ------------------------------------------------------------------------- *
22 * UniqueDeviceName *
23 * *
24 * Check (and, if necessary, modify) the specified print device name to make *
25 * sure it is unique. *
26 * *
27 * ARGUMENTS: *
28 * PSZ pszName : Pointer to device name buffer (9-byte CHAR buffer) *
29 * *
30 * RETURNS: ULONG *
31 * 0 on success, 1 if no unique name could be generated, or the return *
32 * code from SplEnumDevice() if an error occurred. *
33 * ------------------------------------------------------------------------- */
34ULONG UniqueDeviceName( PSZ pszName )
35{
36 PBYTE pBuf;
37 PPRDINFO3 pprd3;
38 CHAR szNumber[ MAX_DEVICE_NAME ] = {0};
39 ULONG i, n, pos,
40 ulNumber = 0,
41 ulAvail = 0,
42 cbBuf = 0;
43 BOOL fUnique = FALSE;
44 SPLERR rc;
45
46
47 rc = SplEnumDevice( NULL, 3, NULL, 0, &ulNumber, &ulAvail, &cbBuf, NULL );
48 if ( rc == ERROR_MORE_DATA || rc == NERR_BufTooSmall ) {
49 pBuf = malloc( cbBuf );
50 if ( pBuf ) {
51 rc = SplEnumDevice( NULL, 3, pBuf, cbBuf, &ulNumber, &ulAvail, &cbBuf, NULL );
52 if ( rc == NO_ERROR ) {
53 n = 1;
54 while ( !fUnique && ( n < 999 )) { // max 999 as a sanity check
55 for ( i = 0; i < ulNumber; i++ ) {
56 pprd3 = (PPRDINFO3) pBuf + i;
57 if ( stricmp( pszName, pprd3->pszPrinterName ) == 0 ) break;
58 }
59 if ( i >= ulNumber ) fUnique = TRUE;
60 else {
61 sprintf( szNumber, "%u", n++ );
62 pos = strlen( pszName ) - strlen( szNumber );
63 pszName[ pos ] = '\0';
64 strncat( pszName, szNumber, MAX_DEVICE_NAME-1 );
65 }
66 }
67 }
68 free( pBuf );
69 if ( rc == NO_ERROR && !fUnique ) return 1;
70 }
71 }
72 else if ( rc == NO_ERROR ) fUnique = TRUE;
73 return rc;
74}
75
76
77/* ------------------------------------------------------------------------- *
78 * ------------------------------------------------------------------------- */
79int main( int argc, char *argv[] )
80{
81 PRDINFO3 devinfo = {0};
82 PRQINFO3 qinfo = {0};
83 ULONG cbBuf = 0;
84 PSZ pszQueueName = NULL,
85 pszPortName = NULL,
86 pszModel = NULL,
87 pszTitle = NULL;
88 CHAR szDeviceName[ 9 ] = {0};
89 USHORT i, len;
90 SPLERR rc;
91
92 if ( argc != 5 ) {
93 printf("PRNTOBJ - Create a new spooled (local) printer object.\n");
94 printf("Syntax: prntobj <queuename> <port> <driver.model> <title>\n");
95 printf(" <queuename> Physical name of the spooler queue to create (max. 8 chars)\n");
96 printf(" <port> The output port to use, e.g. \"LPT2\" (must exist)\n");
97 printf(" <driver.model> The printer driver/model, e.g. \"PSCRIPT.HP Laserjet 4L\"\n");
98 printf(" <title> Descriptive name to use as the object title\n");
99 printf("\nExample:\n prntobj GenericP SLPR2 \"PSCRIPT.Generic Postscript Printer\" \"My Printer\"\n");
100 return 1;
101 }
102 pszQueueName = argv[ 1 ];
103 pszPortName = strupr( argv[ 2 ] );
104 pszModel = argv[ 3 ];
105 pszTitle = argv[ 4 ];
106
107 // Generate a suitable internal device name
108 strncpy( szDeviceName, pszQueueName, MAX_DEVICE_NAME-1 );
109 if (( rc = UniqueDeviceName( szDeviceName )) != NO_ERROR ) {
110 printf("Failed to get unique device name: rc=%u\n", rc);
111 return rc;
112 }
113
114 devinfo.pszPrinterName = szDeviceName;
115 devinfo.pszUserName = NULL;
116 devinfo.pszLogAddr = pszPortName;
117 devinfo.pszComment = pszTitle;
118 devinfo.pszDrivers = pszModel;
119 devinfo.usTimeOut = 45;
120 printf("Creating print device %s for port %s\n", szDeviceName, pszPortName );
121 rc = SplCreateDevice( NULL, 3, &devinfo, sizeof( devinfo ));
122 if ( rc != NO_ERROR ) {
123 printf("Failed to create device: SplCreateDevice() returned %u\n", rc);
124 return rc;
125 }
126
127 // Create the queue
128 if ( strlen( pszQueueName ) > 8 ) pszQueueName[8] = '\0';
129 qinfo.pszName = pszQueueName;
130 qinfo.uPriority = PRQ_DEF_PRIORITY;
131 qinfo.fsType = PRQ3_TYPE_RAW;
132 qinfo.pszPrProc = "PMPRINT";
133 qinfo.pszComment = pszTitle;
134 qinfo.pszPrinters = szDeviceName;
135 qinfo.pszDriverName = pszModel;
136 printf("Creating printer queue %s\n", pszQueueName );
137 rc = SplCreateQueue( NULL, 3, &qinfo, sizeof( qinfo ));
138 if ( rc != NO_ERROR ) {
139 printf("Failed to create printer: SplCreateQueue() returned %u", rc);
140 SplDeleteDevice( NULL, szDeviceName );
141 return rc;
142 }
143
144 return 0;
145}
146
147
Note: See TracBrowser for help on using the repository browser.