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

Last change on this file was 52, checked in by Alex Taylor, 9 years ago

Add sanity checks to prevent some exception conditions.

File size: 6.9 KB
RevLine 
[25]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
[52]12#define INCL_WINSHELLDATA
[25]13#include <os2.h>
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17
18
19#define MAX_DEVICE_NAME 9
20
[52]21/* Various defines copied from CUPS.PDR...
22 */
23#define STR_LEN_PORTNAME 12
24#define STR_LEN_INIVAL 256
25#define STR_LEN_HOSTNAME 64
26#define STR_LEN_QUEUENAME 64
[25]27
[52]28#define APPNAME_LEAD_STR "PM_"
29#define APPNAME_PM_PORT_DRIVER "PM_PORT_DRIVER"
30#define APPNAME_PM_SPOOLER_PORT "PM_SPOOLER_PORT"
31
32#define KEY_DESCRIPTION "DESCRIPTION"
33#define KEY_INITIALIZATION "INITIALIZATION"
34#define KEY_TERMINATION "TERMINATION"
35#define KEY_PORTDRIVER "PORTDRIVER"
36#define KEY_TIMEOUT "TIMEOUT"
37
38
[26]39/* ------------------------------------------------------------------------- *
40 * UniqueDeviceName *
41 * *
42 * Check (and, if necessary, modify) the specified print device name to make *
43 * sure it is unique. *
44 * *
45 * ARGUMENTS: *
46 * PSZ pszName : Pointer to device name buffer (9-byte CHAR buffer) *
47 * *
48 * RETURNS: ULONG *
49 * 0 on success, 1 if no unique name could be generated, or the return *
50 * code from SplEnumDevice() if an error occurred. *
51 * ------------------------------------------------------------------------- */
52ULONG UniqueDeviceName( PSZ pszName )
53{
54 PBYTE pBuf;
55 PPRDINFO3 pprd3;
56 CHAR szNumber[ MAX_DEVICE_NAME ] = {0};
57 ULONG i, n, pos,
58 ulNumber = 0,
59 ulAvail = 0,
60 cbBuf = 0;
61 BOOL fUnique = FALSE;
62 SPLERR rc;
63
64
65 rc = SplEnumDevice( NULL, 3, NULL, 0, &ulNumber, &ulAvail, &cbBuf, NULL );
66 if ( rc == ERROR_MORE_DATA || rc == NERR_BufTooSmall ) {
67 pBuf = malloc( cbBuf );
68 if ( pBuf ) {
69 rc = SplEnumDevice( NULL, 3, pBuf, cbBuf, &ulNumber, &ulAvail, &cbBuf, NULL );
70 if ( rc == NO_ERROR ) {
71 n = 1;
72 while ( !fUnique && ( n < 999 )) { // max 999 as a sanity check
73 for ( i = 0; i < ulNumber; i++ ) {
74 pprd3 = (PPRDINFO3) pBuf + i;
75 if ( stricmp( pszName, pprd3->pszPrinterName ) == 0 ) break;
76 }
77 if ( i >= ulNumber ) fUnique = TRUE;
78 else {
79 sprintf( szNumber, "%u", n++ );
80 pos = strlen( pszName ) - strlen( szNumber );
81 pszName[ pos ] = '\0';
82 strncat( pszName, szNumber, MAX_DEVICE_NAME-1 );
83 }
84 }
85 }
86 free( pBuf );
[28]87 if ( rc == NO_ERROR && !fUnique ) return 1;
[26]88 }
89 }
[28]90 else if ( rc == NO_ERROR ) fUnique = TRUE;
91 return rc;
[26]92}
93
94
95/* ------------------------------------------------------------------------- *
96 * ------------------------------------------------------------------------- */
[25]97int main( int argc, char *argv[] )
98{
99 PRDINFO3 devinfo = {0};
100 PRQINFO3 qinfo = {0};
101 ULONG cbBuf = 0;
102 PSZ pszQueueName = NULL,
103 pszPortName = NULL,
104 pszModel = NULL,
105 pszTitle = NULL;
106 CHAR szDeviceName[ 9 ] = {0};
[52]107 CHAR szIniApp[ STR_LEN_PORTNAME ] = {0};
108 CHAR szIniVal[ STR_LEN_INIVAL ] = {0};
109 USHORT i;
[25]110 SPLERR rc;
111
112 if ( argc != 5 ) {
113 printf("PRNTOBJ - Create a new spooled (local) printer object.\n");
114 printf("Syntax: prntobj <queuename> <port> <driver.model> <title>\n");
115 printf(" <queuename> Physical name of the spooler queue to create (max. 8 chars)\n");
116 printf(" <port> The output port to use, e.g. \"LPT2\" (must exist)\n");
117 printf(" <driver.model> The printer driver/model, e.g. \"PSCRIPT.HP Laserjet 4L\"\n");
118 printf(" <title> Descriptive name to use as the object title\n");
[29]119 printf("\nExample:\n prntobj GenericP SLPR2 \"PSCRIPT.Generic Postscript Printer\" \"My Printer\"\n");
[25]120 return 1;
121 }
122 pszQueueName = argv[ 1 ];
123 pszPortName = strupr( argv[ 2 ] );
124 pszModel = argv[ 3 ];
125 pszTitle = argv[ 4 ];
[52]126 if ( strlen( pszTitle ) > 47 )
127 pszTitle[ 47 ] = '\0';
[25]128
[52]129 // Make sure the specified port exists and has valid settings.
130 strcpy( szIniApp, APPNAME_LEAD_STR );
131 strncat( szIniApp, pszPortName, STR_LEN_PORTNAME-1 );
132 rc = PrfQueryProfileString( HINI_SYSTEMPROFILE, szIniApp,
133 KEY_PORTDRIVER, NULL,
134 (PVOID) szIniVal, STR_LEN_INIVAL );
135 if ( rc < 2 ) {
136 printf("Error: %s does not appear to be a valid port.\n", pszPortName );
137 return ( 1 );
138 }
139
[25]140 // Generate a suitable internal device name
141 strncpy( szDeviceName, pszQueueName, MAX_DEVICE_NAME-1 );
[26]142 if (( rc = UniqueDeviceName( szDeviceName )) != NO_ERROR ) {
143 printf("Failed to get unique device name: rc=%u\n", rc);
[28]144 return rc;
[25]145 }
[26]146
147 devinfo.pszPrinterName = szDeviceName;
148 devinfo.pszUserName = NULL;
149 devinfo.pszLogAddr = pszPortName;
150 devinfo.pszComment = pszTitle;
151 devinfo.pszDrivers = pszModel;
152 devinfo.usTimeOut = 45;
[29]153 printf("Creating print device %s for port %s\n", szDeviceName, pszPortName );
[26]154 rc = SplCreateDevice( NULL, 3, &devinfo, sizeof( devinfo ));
155 if ( rc != NO_ERROR ) {
156 printf("Failed to create device: SplCreateDevice() returned %u\n", rc);
157 return rc;
[25]158 }
159
160 // Create the queue
[29]161 if ( strlen( pszQueueName ) > 8 ) pszQueueName[8] = '\0';
[25]162 qinfo.pszName = pszQueueName;
163 qinfo.uPriority = PRQ_DEF_PRIORITY;
164 qinfo.fsType = PRQ3_TYPE_RAW;
165 qinfo.pszPrProc = "PMPRINT";
166 qinfo.pszComment = pszTitle;
167 qinfo.pszPrinters = szDeviceName;
168 qinfo.pszDriverName = pszModel;
[29]169 printf("Creating printer queue %s\n", pszQueueName );
[25]170 rc = SplCreateQueue( NULL, 3, &qinfo, sizeof( qinfo ));
171 if ( rc != NO_ERROR ) {
172 printf("Failed to create printer: SplCreateQueue() returned %u", rc);
173 SplDeleteDevice( NULL, szDeviceName );
[26]174 return rc;
[25]175 }
176
177 return 0;
178}
179
180
Note: See TracBrowser for help on using the repository browser.