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

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

Improved logic for generating unique device name.

File size: 5.4 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 }
70 }
71 if ( rc == NO_ERROR && !fUnique ) return 1;
72 else return rc;
73}
74
75
76/* ------------------------------------------------------------------------- *
77 * ------------------------------------------------------------------------- */
78int main( int argc, char *argv[] )
79{
80 PRDINFO3 devinfo = {0};
81 PRQINFO3 qinfo = {0};
82 ULONG cbBuf = 0;
83 PSZ pszQueueName = NULL,
84 pszPortName = NULL,
85 pszModel = NULL,
86 pszTitle = NULL;
87 CHAR szDeviceName[ 9 ] = {0};
88 USHORT i, len;
89 SPLERR rc;
90
91 if ( argc != 5 ) {
92 printf("PRNTOBJ - Create a new spooled (local) printer object.\n");
93 printf("Syntax: prntobj <queuename> <port> <driver.model> <title>\n");
94 printf(" <queuename> Physical name of the spooler queue to create (max. 8 chars)\n");
95 printf(" <port> The output port to use, e.g. \"LPT2\" (must exist)\n");
96 printf(" <driver.model> The printer driver/model, e.g. \"PSCRIPT.HP Laserjet 4L\"\n");
97 printf(" <title> Descriptive name to use as the object title\n");
98 printf("\nExample:\n prntobj GENERICP SLPR2 \"PSCRIPT.Generic Postscript Printer\" \"My Printer\"\n");
99 return 1;
100 }
101 pszQueueName = argv[ 1 ];
102 pszPortName = strupr( argv[ 2 ] );
103 pszModel = argv[ 3 ];
104 pszTitle = argv[ 4 ];
105
106 // Generate a suitable internal device name
107 strncpy( szDeviceName, pszQueueName, MAX_DEVICE_NAME-1 );
108 if (( rc = UniqueDeviceName( szDeviceName )) != NO_ERROR ) {
109 printf("Failed to get unique device name: rc=%u\n", rc);
110 return 0;
111 }
112
113 devinfo.pszPrinterName = szDeviceName;
114 devinfo.pszUserName = NULL;
115 devinfo.pszLogAddr = pszPortName;
116 devinfo.pszComment = pszTitle;
117 devinfo.pszDrivers = pszModel;
118 devinfo.usTimeOut = 45;
119 rc = SplCreateDevice( NULL, 3, &devinfo, sizeof( devinfo ));
120 if ( rc != NO_ERROR ) {
121 printf("Failed to create device: SplCreateDevice() returned %u\n", rc);
122 return rc;
123 }
124
125 // Create the queue
126 qinfo.pszName = pszQueueName;
127 qinfo.uPriority = PRQ_DEF_PRIORITY;
128 qinfo.fsType = PRQ3_TYPE_RAW;
129 qinfo.pszPrProc = "PMPRINT";
130 qinfo.pszComment = pszTitle;
131 qinfo.pszPrinters = szDeviceName;
132 qinfo.pszDriverName = pszModel;
133 rc = SplCreateQueue( NULL, 3, &qinfo, sizeof( qinfo ));
134 if ( rc != NO_ERROR ) {
135 printf("Failed to create printer: SplCreateQueue() returned %u", rc);
136 SplDeleteDevice( NULL, szDeviceName );
137 return rc;
138 }
139
140 return 0;
141}
142
143
Note: See TracBrowser for help on using the repository browser.