| 1 | /************************************************** |
| 2 | * SQLConfigDataSource |
| 3 | * |
| 4 | * Determine the DriverSetup file name and then try to pass |
| 5 | * the work along to its ConfigDSN(). |
| 6 | * |
| 7 | ************************************************** |
| 8 | * This code was created by Peter Harvey @ CodeByDesign. |
| 9 | * Released under LGPL 28.JAN.99 |
| 10 | * |
| 11 | * Contributions from... |
| 12 | * ----------------------------------------------- |
| 13 | * Peter Harvey - pharvey@codebydesign.com |
| 14 | **************************************************/ |
| 15 | #include <config.h> |
| 16 | #include <odbcinstext.h> |
| 17 | |
| 18 | static BOOL SQLConfigDataSourceWide( HWND hWnd, |
| 19 | WORD nRequest, |
| 20 | LPCSTR pszDriver, /* USER FRIENDLY NAME (not file name) */ |
| 21 | LPCSTR pszAttributes, |
| 22 | LPCWSTR pszDriverW, |
| 23 | LPCWSTR pszAttributesW ) |
| 24 | { |
| 25 | BOOL (*pFunc)( HWND, WORD, LPCSTR, LPCSTR ); |
| 26 | BOOL (*pFuncW)( HWND, WORD, LPCWSTR, LPCWSTR ); |
| 27 | BOOL nReturn; |
| 28 | void *hDLL = FALSE; |
| 29 | HINI hIni; |
| 30 | char szDriverSetup[INI_MAX_PROPERTY_VALUE+1]; |
| 31 | char szIniName[ ODBC_FILENAME_MAX * 2 + 3 ]; |
| 32 | char b1[ ODBC_FILENAME_MAX + 1 ], b2[ ODBC_FILENAME_MAX + 1 ]; |
| 33 | |
| 34 | /* SANITY CHECKS */ |
| 35 | if ( pszDriver == NULL || pszAttributes == NULL ) |
| 36 | { |
| 37 | inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" ); |
| 38 | return FALSE; |
| 39 | } |
| 40 | |
| 41 | if ( pszDriver[0] == '\0' ) |
| 42 | { |
| 43 | inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" ); |
| 44 | return FALSE; |
| 45 | } |
| 46 | switch ( nRequest ) |
| 47 | { |
| 48 | case ODBC_ADD_DSN: |
| 49 | case ODBC_CONFIG_DSN: |
| 50 | case ODBC_REMOVE_DSN: |
| 51 | case ODBC_ADD_SYS_DSN: |
| 52 | case ODBC_CONFIG_SYS_DSN: |
| 53 | case ODBC_REMOVE_SYS_DSN: |
| 54 | case ODBC_REMOVE_DEFAULT_DSN: |
| 55 | break; |
| 56 | default: |
| 57 | inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_INVALID_REQUEST_TYPE, "" ); |
| 58 | return FALSE; |
| 59 | } |
| 60 | |
| 61 | #ifdef VMS |
| 62 | sprintf( szIniName, "%s:%s" , odbcinst_system_file_path( b1 ), odbcinst_system_file_name( b2 ) ); |
| 63 | #else |
| 64 | sprintf( szIniName, "%s/%s" , odbcinst_system_file_path( b1 ), odbcinst_system_file_name( b2 ) ); |
| 65 | #endif |
| 66 | |
| 67 | /* OK */ |
| 68 | #ifdef __OS2__ |
| 69 | if ( iniOpen( &hIni, szIniName, "#;" , '[', ']', '=', TRUE, 1L ) != INI_SUCCESS ) |
| 70 | #else |
| 71 | if ( iniOpen( &hIni, szIniName, "#;" , '[', ']', '=', TRUE ) != INI_SUCCESS ) |
| 72 | #endif |
| 73 | { |
| 74 | inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" ); |
| 75 | return FALSE; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * initialize libtool |
| 80 | */ |
| 81 | |
| 82 | lt_dlinit(); |
| 83 | |
| 84 | #ifdef PLATFORM64 |
| 85 | if ( iniPropertySeek( hIni, (char *)pszDriver, "Setup64" , "" ) == INI_SUCCESS || |
| 86 | iniPropertySeek( hIni, (char *)pszDriver, "Setup" , "" ) == INI_SUCCESS ) |
| 87 | #else |
| 88 | if ( iniPropertySeek( hIni, (char *)pszDriver, "Setup" , "" ) == INI_SUCCESS ) |
| 89 | #endif |
| 90 | { |
| 91 | iniValue( hIni, szDriverSetup ); |
| 92 | |
| 93 | iniClose( hIni ); |
| 94 | |
| 95 | if ( szDriverSetup[ 0 ] == '\0' ) |
| 96 | { |
| 97 | char szError[ 512 ]; |
| 98 | sprintf( szError, "Could not find Setup property for (%.400s) in system information" , pszDriver ); |
| 99 | inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, szError ); |
| 100 | __set_config_mode( ODBC_BOTH_DSN ); |
| 101 | return FALSE; |
| 102 | } |
| 103 | |
| 104 | nReturn = FALSE; |
| 105 | if ( (hDLL = lt_dlopen( szDriverSetup )) ) |
| 106 | { |
| 107 | pFunc = (BOOL (*)(HWND, WORD, LPCSTR, LPCSTR )) lt_dlsym( hDLL, "ConfigDSN" ); |
| 108 | pFuncW = (BOOL (*)(HWND, WORD, LPCWSTR, LPCWSTR )) lt_dlsym( hDLL, "ConfigDSNW" ); |
| 109 | if ( pFunc ) |
| 110 | { |
| 111 | /* |
| 112 | * set the mode |
| 113 | */ |
| 114 | switch ( nRequest ) |
| 115 | { |
| 116 | case ODBC_ADD_DSN: |
| 117 | case ODBC_CONFIG_DSN: |
| 118 | case ODBC_REMOVE_DSN: |
| 119 | case ODBC_REMOVE_DEFAULT_DSN: |
| 120 | __set_config_mode( ODBC_USER_DSN ); |
| 121 | break; |
| 122 | |
| 123 | case ODBC_ADD_SYS_DSN: |
| 124 | __set_config_mode( ODBC_SYSTEM_DSN ); |
| 125 | nRequest = ODBC_ADD_DSN; |
| 126 | break; |
| 127 | |
| 128 | case ODBC_CONFIG_SYS_DSN: |
| 129 | __set_config_mode( ODBC_SYSTEM_DSN ); |
| 130 | nRequest = ODBC_CONFIG_DSN; |
| 131 | break; |
| 132 | |
| 133 | case ODBC_REMOVE_SYS_DSN: |
| 134 | __set_config_mode( ODBC_SYSTEM_DSN ); |
| 135 | nRequest = ODBC_REMOVE_DSN; |
| 136 | break; |
| 137 | } |
| 138 | nReturn = pFunc( hWnd, nRequest, pszDriver, pszAttributes ); |
| 139 | } |
| 140 | else if ( pFuncW ) |
| 141 | { |
| 142 | /* |
| 143 | * set the mode |
| 144 | */ |
| 145 | switch ( nRequest ) |
| 146 | { |
| 147 | case ODBC_ADD_DSN: |
| 148 | case ODBC_CONFIG_DSN: |
| 149 | case ODBC_REMOVE_DSN: |
| 150 | case ODBC_REMOVE_DEFAULT_DSN: |
| 151 | __set_config_mode( ODBC_USER_DSN ); |
| 152 | break; |
| 153 | |
| 154 | case ODBC_ADD_SYS_DSN: |
| 155 | __set_config_mode( ODBC_SYSTEM_DSN ); |
| 156 | nRequest = ODBC_ADD_DSN; |
| 157 | break; |
| 158 | |
| 159 | case ODBC_CONFIG_SYS_DSN: |
| 160 | __set_config_mode( ODBC_SYSTEM_DSN ); |
| 161 | nRequest = ODBC_CONFIG_DSN; |
| 162 | break; |
| 163 | |
| 164 | case ODBC_REMOVE_SYS_DSN: |
| 165 | __set_config_mode( ODBC_SYSTEM_DSN ); |
| 166 | nRequest = ODBC_REMOVE_DSN; |
| 167 | break; |
| 168 | } |
| 169 | nReturn = pFuncW( hWnd, nRequest, pszDriverW, pszAttributesW ); |
| 170 | } |
| 171 | else |
| 172 | { |
| 173 | inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" ); |
| 174 | } |
| 175 | } |
| 176 | else |
| 177 | inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" ); |
| 178 | |
| 179 | __set_config_mode( ODBC_BOTH_DSN ); |
| 180 | return nReturn; |
| 181 | |
| 182 | } |
| 183 | |
| 184 | inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" ); |
| 185 | iniClose( hIni ); |
| 186 | |
| 187 | __set_config_mode( ODBC_BOTH_DSN ); |
| 188 | |
| 189 | return FALSE; |
| 190 | } |
| 191 | |
| 192 | BOOL INSTAPI SQLConfigDataSourceW (HWND hwndParent, |
| 193 | WORD fRequest, |
| 194 | LPCWSTR lpszDriver, |
| 195 | LPCWSTR lpszAttributes) |
| 196 | { |
| 197 | char *drv, *attr; |
| 198 | BOOL ret; |
| 199 | |
| 200 | inst_logClear(); |
| 201 | |
| 202 | drv = _single_string_alloc_and_copy( lpszDriver ); |
| 203 | attr = _multi_string_alloc_and_copy( lpszAttributes ); |
| 204 | |
| 205 | ret = SQLConfigDataSourceWide( hwndParent, fRequest, drv, attr, lpszDriver, lpszAttributes ); |
| 206 | |
| 207 | free( drv ); |
| 208 | free( attr ); |
| 209 | |
| 210 | return ret; |
| 211 | } |
| 212 | |
| 213 | BOOL INSTAPI SQLConfigDataSource (HWND hwndParent, |
| 214 | WORD fRequest, |
| 215 | LPCSTR lpszDriver, |
| 216 | LPCSTR lpszAttributes) |
| 217 | { |
| 218 | SQLWCHAR *drv, *attr; |
| 219 | BOOL ret; |
| 220 | |
| 221 | inst_logClear(); |
| 222 | |
| 223 | drv = _single_string_alloc_and_expand( lpszDriver ); |
| 224 | attr = _multi_string_alloc_and_expand( lpszAttributes ); |
| 225 | |
| 226 | ret = SQLConfigDataSourceWide( hwndParent, fRequest, lpszDriver, lpszAttributes, drv, attr ); |
| 227 | |
| 228 | free( drv ); |
| 229 | free( attr ); |
| 230 | |
| 231 | return ret; |
| 232 | } |
| 233 | |