| 1 | /* CUPSPORT
|
|---|
| 2 | *
|
|---|
| 3 | * Simple program to create a new CUPS printer port pointing to the specified
|
|---|
| 4 | * CUPSD host and queue.
|
|---|
| 5 | *
|
|---|
| 6 | * Syntax: cupsport <portname> <hostname> <queuename>
|
|---|
| 7 | */
|
|---|
| 8 |
|
|---|
| 9 | #define INCL_DOSERRORS
|
|---|
| 10 | #define INCL_DOSMODULEMGR
|
|---|
| 11 | #define INCL_DOSPROCESS
|
|---|
| 12 | #define INCL_GPI
|
|---|
| 13 | #define INCL_SPL
|
|---|
| 14 | #define INCL_SPLERRORS
|
|---|
| 15 | #define INCL_WIN
|
|---|
| 16 | #include <os2.h>
|
|---|
| 17 | #include <stdio.h>
|
|---|
| 18 | #include <stdlib.h>
|
|---|
| 19 | #include <string.h>
|
|---|
| 20 |
|
|---|
| 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
|
|---|
| 27 |
|
|---|
| 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 | /* Driver-specific port setting structure for SplPdSet & SplPdQuery.
|
|---|
| 39 | */
|
|---|
| 40 | typedef struct _PORTSETTINGS {
|
|---|
| 41 | CHAR szHost[ STR_LEN_HOSTNAME + 1 ];
|
|---|
| 42 | CHAR szQueue[ STR_LEN_QUEUENAME + 1 ];
|
|---|
| 43 | } PORTSETTINGS, *PPORTSETTINGS;
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 | /* Various items that should be defined in the toolkit header pmsplb.h,
|
|---|
| 47 | * if it actually existed...
|
|---|
| 48 | */
|
|---|
| 49 | #define TYPE_LONG_WAIT 2
|
|---|
| 50 | #define BIDI_SET_PORTDRV 0x19
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 | int main( int argc, char *argv[] )
|
|---|
| 54 | {
|
|---|
| 55 | PORTSETTINGS settings;
|
|---|
| 56 |
|
|---|
| 57 | HAB hab;
|
|---|
| 58 | ULONG ulCmp,
|
|---|
| 59 | ulRC;
|
|---|
| 60 | PSZ pszPortName = NULL,
|
|---|
| 61 | pszHostName = NULL,
|
|---|
| 62 | pszPrinterName = NULL,
|
|---|
| 63 | pszToken;
|
|---|
| 64 | CHAR szPathName[ CCHMAXPATH + 1 ];
|
|---|
| 65 | CHAR szIniApp[ STR_LEN_PORTNAME ];
|
|---|
| 66 | CHAR szIniVal[ STR_LEN_INIVAL ];
|
|---|
| 67 | HMODULE hPdr = NULLHANDLE;
|
|---|
| 68 | PFN pfn_CupsInstallPort,
|
|---|
| 69 | pfn_CupsSet;
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 | if ( argc < 4 ) {
|
|---|
| 73 | printf("CUPSPORT - Create a new eCups printer port for the specified CUPS host/printer.\n");
|
|---|
| 74 | printf("Syntax: cupsport <port-name> <host> <printer>\n");
|
|---|
| 75 | printf(" <port-name> Name of the new port to create (CUPS<x>, where <x> = 1-64)\n");
|
|---|
| 76 | printf(" <host> Name of the CUPS host where the destination printer is defined\n");
|
|---|
| 77 | printf(" <printer> Name of the destination printer (as defined in CUPS) on <host>\n");
|
|---|
| 78 | printf("\nExample: cupsport CUPS3 localhost PIXMA-1\n");
|
|---|
| 79 | return 1;
|
|---|
| 80 | }
|
|---|
| 81 | pszPortName = strupr( argv[ 1 ] );
|
|---|
| 82 | pszHostName = argv[ 2 ];
|
|---|
| 83 | pszPrinterName = argv[ 3 ];
|
|---|
| 84 |
|
|---|
| 85 | /* Get the path to the installed CUPS.PDR */
|
|---|
| 86 | hab = WinInitialize( 0 );
|
|---|
| 87 | ulRC = PrfQueryProfileString( HINI_SYSTEMPROFILE, APPNAME_PM_PORT_DRIVER,
|
|---|
| 88 | "CUPS", NULL, (PVOID) szPathName, CCHMAXPATH );
|
|---|
| 89 | if ( !ulRC ) {
|
|---|
| 90 | ulRC = ulRC = WinGetLastError( hab );
|
|---|
| 91 | printf("CUPS port driver is not installed.\n");
|
|---|
| 92 | WinTerminate( hab );
|
|---|
| 93 | return ulRC;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | /* Now load the port driver DLL and register the install and setup functions */
|
|---|
| 97 | ulRC = DosLoadModule( NULL, 0, szPathName, &hPdr );
|
|---|
| 98 | if ( ulRC != NO_ERROR ) {
|
|---|
| 99 | printf("Failed to load CUPS.PDR, RC=%u.\n", ulRC );
|
|---|
| 100 | WinTerminate( hab );
|
|---|
| 101 | return ulRC;
|
|---|
| 102 | }
|
|---|
| 103 | ulRC = DosQueryProcAddr( hPdr, 0, "SPLPDINSTALLPORT", &pfn_CupsInstallPort );
|
|---|
| 104 | if ( ulRC != NO_ERROR ) {
|
|---|
| 105 | printf("Failed to resolve proc address, RC=%u.\n", ulRC );
|
|---|
| 106 | goto finish;
|
|---|
| 107 | }
|
|---|
| 108 | ulRC = DosQueryProcAddr( hPdr, 0, "SPLPDSET", &pfn_CupsSet );
|
|---|
| 109 | if ( ulRC != NO_ERROR ) {
|
|---|
| 110 | printf("Failed to resolve proc address, RC=%u.\n", ulRC );
|
|---|
| 111 | goto finish;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | /* Create the new port (it will be given default settings by the PDR) */
|
|---|
| 115 | ulRC = pfn_CupsInstallPort( hab, pszPortName );
|
|---|
| 116 | if ( ulRC != NO_ERROR ) {
|
|---|
| 117 | printf("Failed to create port, RC=0x%X.\n", ulRC );
|
|---|
| 118 | goto finish;
|
|---|
| 119 | }
|
|---|
| 120 | printf("Port %s was created successfully.\n", pszPortName );
|
|---|
| 121 |
|
|---|
| 122 | /* Now update the port driver settings */
|
|---|
| 123 | strncpy( settings.szHost, pszHostName, STR_LEN_HOSTNAME );
|
|---|
| 124 | strncpy( settings.szQueue, pszPrinterName, STR_LEN_QUEUENAME );
|
|---|
| 125 | ulRC = pfn_CupsSet( pszPortName, TYPE_LONG_WAIT, BIDI_SET_PORTDRV,
|
|---|
| 126 | &settings, sizeof( settings ));
|
|---|
| 127 | if ( ulRC ) {
|
|---|
| 128 | printf("Failed to update port settings, RC=0x%X.\n", ulRC );
|
|---|
| 129 | goto finish;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | /* Make sure the settings actually were updated (older versions of CUPS.PDR
|
|---|
| 133 | * don't do anything when SplPdSet is called, but return success anyway).
|
|---|
| 134 | */
|
|---|
| 135 | strcpy( szIniApp, APPNAME_LEAD_STR );
|
|---|
| 136 | strncat( szIniApp, pszPortName, STR_LEN_PORTNAME-1 );
|
|---|
| 137 | ulRC = PrfQueryProfileString( HINI_SYSTEMPROFILE, szIniApp,
|
|---|
| 138 | KEY_INITIALIZATION, NULL,
|
|---|
| 139 | (PVOID) szIniVal, STR_LEN_INIVAL );
|
|---|
| 140 | if ( !ulRC ) {
|
|---|
| 141 | ulRC = 1;
|
|---|
| 142 | printf("Failed to get CUPS port settings for %s: 0x%X\n", szIniApp, WinGetLastError( hab ));
|
|---|
| 143 | goto finish;
|
|---|
| 144 | }
|
|---|
| 145 | pszToken = strtok( szIniVal, "#");
|
|---|
| 146 | if ( pszToken ) {
|
|---|
| 147 | ulCmp = strcmp( pszToken, pszHostName );
|
|---|
| 148 | pszToken = strtok( NULL, "#");
|
|---|
| 149 | if ( pszToken )
|
|---|
| 150 | ulCmp += pszToken ? strcmp( pszToken, pszPrinterName ) : 1;
|
|---|
| 151 | }
|
|---|
| 152 | else ulCmp = 2;
|
|---|
| 153 |
|
|---|
| 154 | if ( ulCmp ) {
|
|---|
| 155 | printf("Unable to set CUPS port settings.\n");
|
|---|
| 156 | ulRC = 1;
|
|---|
| 157 | }
|
|---|
| 158 | else {
|
|---|
| 159 | printf("Port settings were updated successfully.\n");
|
|---|
| 160 | ulRC = 0;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | finish:
|
|---|
| 164 | if ( hPdr ) DosFreeModule( hPdr );
|
|---|
| 165 | WinTerminate( hab );
|
|---|
| 166 |
|
|---|
| 167 | return ulRC;
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|