File: ODBCINSTConstructProperties.c

package info (click to toggle)
unixodbc 2.3.6-0.1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 11,476 kB
  • sloc: ansic: 92,148; sh: 11,928; makefile: 1,630; yacc: 969; sql: 1
file content (251 lines) | stat: -rw-r--r-- 9,606 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/**************************************************
 * ODBCINSTConstructProperties
 *
 **************************************************
 * This code was created by Peter Harvey @ CodeByDesign.
 * Released under LGPL 28.JAN.99
 *
 * Contributions from...
 * -----------------------------------------------
 * Peter Harvey		- pharvey@codebydesign.com
 **************************************************/
#include <config.h>
#include <odbcinstext.h>

/*
static const char *aYesNo[] =
{
	"Yes",
	"No",
	NULL
};
*/

/*! 
 * \brief   Builds a property list for pszDriver.
 *
 *          Adds common DSN properties (Name,Driver,Description) and then asks the
 *          drivers setup to load any additional properties.
 *
 *          This is used to support editing DSN properties without forcing the driver
 *          developer to create a UI for the many different UI implementations. The
 *          driver developer can just implement ODBCINSTGetProperties. This function
 *          can then call ODBCINSTGetProperties to get properties. The code that calls
 *          this function can then display the properties in the UI in use.
 * 
 * \param   pszDriver       Friendly driver name.
 * \param   hFirstProperty  Place to store the properties list. The properties (including
 *                          some of the elements within each HODBCINSTPROPERTY may
 *                          need to be freed using \sa ODBCINSTDestructProperties.
 * 
 * \return  int
 * \retval  ODBCINST_ERROR      Called failed. No memory was allocated at hFirstProperty. The
 *                              likely reasons for this; \li failed to lookup setup library name 
 *                              \li failed to load setup library \li failed to find 
 *                              ODBCINSTGetProperties symbol in setup library
 * \retval  ODBCINST_SUCCESS    Success! Do not forget to call ODBCINSTDestructProperties to
 *                              free memory used by the properties when you are done.
 *
 * \sa      ODBCINSTDestructProperties
 */
int ODBCINSTConstructProperties( char *pszDriver, HODBCINSTPROPERTY *hFirstProperty )
{
	char 				szError[LOG_MSG_MAX+1];
	char 				szDriverSetup[ODBC_FILENAME_MAX+1];
	HINI 				hIni;
	int					(*pODBCINSTGetProperties)( HODBCINSTPROPERTY );
	void 				*hDLL	= NULL;
	HODBCINSTPROPERTY	hLastProperty;
	char				szSectionName[INI_MAX_OBJECT_NAME+1];
    char                szIniName[ ODBC_FILENAME_MAX * 2 + 1 ];
	char				b1[ ODBC_FILENAME_MAX + 1 ], b2[ ODBC_FILENAME_MAX + 1 ];

	/* SANITY CHECKS */
	if ( pszDriver == NULL )
	{
		inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "Need a driver name. Make it the friendly name." );
		return ODBCINST_ERROR;
	}
#ifdef VMS
    sprintf( szIniName, "%s:%s", odbcinst_system_file_path( b1 ), odbcinst_system_file_name( b2 ));
#else
    sprintf( szIniName, "%s/%s", odbcinst_system_file_path( b1 ), odbcinst_system_file_name( b2 ));
#endif

	/* GET DRIVER SETUP FILE NAME FOR GIVEN DRIVER */
#ifdef __OS2__
	if ( iniOpen( &hIni, szIniName, "#;", '[', ']', '=', FALSE, 1L ) != INI_SUCCESS )
#else
	if ( iniOpen( &hIni, szIniName, "#;", '[', ']', '=', FALSE ) != INI_SUCCESS )
#endif
	{
		inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "Could not open odbcinst.ini" );
		return ODBCINST_ERROR;
	}

#ifdef PLATFORM64
	/* ASSUME USER FRIENDLY NAME FOR STARTERS */
	if ( iniPropertySeek( hIni, pszDriver, "Setup64", "" ) == INI_SUCCESS )
	{
	}
	else if ( iniPropertySeek( hIni, pszDriver, "Setup", "" ) == INI_SUCCESS )
	{
	}
	else
	{
		/* NOT USER FRIENDLY NAME I GUESS SO ASSUME DRIVER FILE NAME */
		if ( iniPropertySeek( hIni, "", "Driver64", pszDriver ) == INI_SUCCESS )
		{
			iniObject( hIni, szSectionName );
			if ( iniPropertySeek( hIni, szSectionName, "Setup64", "" ) != INI_SUCCESS )
			{
				sprintf( szError, "Could not find Setup property for (%s) in system information", pszDriver );
				inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, szError );
				iniClose( hIni );
				return ODBCINST_ERROR;
			}
		}
		else if ( iniPropertySeek( hIni, "", "Driver", pszDriver ) == INI_SUCCESS )
		{
			iniObject( hIni, szSectionName );
			if ( iniPropertySeek( hIni, szSectionName, "Setup", "" ) != INI_SUCCESS )
			{
				sprintf( szError, "Could not find Setup property for (%s) in system information", pszDriver );
				inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, szError );
				iniClose( hIni );
				return ODBCINST_ERROR;
			}
		}
		else
		{
			sprintf( szError, "Could not find driver (%s) in system information", pszDriver );
			inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, szError );
			iniClose( hIni );
			return ODBCINST_ERROR;
		}
	}
#else
	/* ASSUME USER FRIENDLY NAME FOR STARTERS */
	if ( iniPropertySeek( hIni, pszDriver, "Setup", "" ) != INI_SUCCESS )
	{
		/* NOT USER FRIENDLY NAME I GUESS SO ASSUME DRIVER FILE NAME */
		if ( iniPropertySeek( hIni, "", "Driver", pszDriver ) != INI_SUCCESS )
		{
			sprintf( szError, "Could not find driver (%s) in system information", pszDriver );
			inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, szError );
			iniClose( hIni );
			return ODBCINST_ERROR;
		}
		else
		{
			iniObject( hIni, szSectionName );
			if ( iniPropertySeek( hIni, szSectionName, "Setup", "" ) != INI_SUCCESS )
			{
				sprintf( szError, "Could not find Setup property for (%s) in system information", pszDriver );
				inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, szError );
				iniClose( hIni );
				return ODBCINST_ERROR;
			}
		}
	}
#endif

	iniValue( hIni, szDriverSetup );
	iniClose( hIni );

	if ( szDriverSetup[ 0 ] == '\0' ) 
	{
		sprintf( szError, "Could not find Setup property for (%s) in system information", pszDriver );
		inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, szError );
		return ODBCINST_ERROR;
	}

    /*
     * initialize libtool
     */

    lt_dlinit();
    lt_dlsetsearchpath(MODULEDIR);

	/* TRY GET FUNC FROM DRIVER SETUP */
	if ( !(hDLL = lt_dlopen( szDriverSetup ))  )
	{
		inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "Could not load library" );
		return ODBCINST_ERROR;
	}

	pODBCINSTGetProperties = (int(*)(struct tODBCINSTPROPERTY*)) lt_dlsym( hDLL, "ODBCINSTGetProperties" );

/*	PAH - This can be true even when we found the symbol.
    if ( lt_dlerror() != NULL ) 
*/
	if ( pODBCINSTGetProperties == NULL )
	{
		inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "Could not find ODBCINSTGetProperties()" );
		return ODBCINST_ERROR;
	}
	
	/* MANDATORY PROPERTIES */
	(*hFirstProperty) 						= (HODBCINSTPROPERTY)malloc( sizeof(ODBCINSTPROPERTY) );
	memset( (*hFirstProperty), 0, sizeof(ODBCINSTPROPERTY) );
	(*hFirstProperty)->nPromptType			= ODBCINST_PROMPTTYPE_TEXTEDIT;
	(*hFirstProperty)->pNext				= NULL;
    (*hFirstProperty)->bRefresh				= 0;
    (*hFirstProperty)->hDLL					= hDLL;
    (*hFirstProperty)->pWidget				= NULL;
    (*hFirstProperty)->pszHelp				= NULL;
	(*hFirstProperty)->aPromptData			= NULL;
	strncpy( (*hFirstProperty)->szName, "Name", INI_MAX_PROPERTY_NAME );
	strcpy( (*hFirstProperty)->szValue, "" );
	hLastProperty = (*hFirstProperty);

	(*hFirstProperty)->pNext 				= (HODBCINSTPROPERTY)malloc( sizeof(ODBCINSTPROPERTY) );
	hLastProperty 							= (*hFirstProperty)->pNext;
	memset( hLastProperty, 0, sizeof(ODBCINSTPROPERTY) );
	hLastProperty->nPromptType				= ODBCINST_PROMPTTYPE_TEXTEDIT;
	hLastProperty->pNext					= NULL;
    hLastProperty->bRefresh					= 0;
    hLastProperty->hDLL						= hDLL;
    hLastProperty->pWidget					= NULL;
    (*hFirstProperty)->pszHelp				= NULL;
	(*hFirstProperty)->aPromptData			= NULL;
	strncpy( hLastProperty->szName, "Description", INI_MAX_PROPERTY_NAME );
	strncpy( hLastProperty->szValue, pszDriver, INI_MAX_PROPERTY_VALUE );

	hLastProperty->pNext 				= (HODBCINSTPROPERTY)malloc( sizeof(ODBCINSTPROPERTY) );
	hLastProperty 							= hLastProperty->pNext;
	memset( hLastProperty, 0, sizeof(ODBCINSTPROPERTY) );
	hLastProperty->nPromptType				= ODBCINST_PROMPTTYPE_LABEL;
	hLastProperty->pNext					= NULL;
    hLastProperty->bRefresh					= 0;
    hLastProperty->hDLL						= hDLL;
    hLastProperty->pWidget					= NULL;
    (*hFirstProperty)->pszHelp				= NULL;
	(*hFirstProperty)->aPromptData			= NULL;
	strncpy( hLastProperty->szName, "Driver", INI_MAX_PROPERTY_NAME );
	strncpy( hLastProperty->szValue, pszDriver, INI_MAX_PROPERTY_VALUE );
/*
	hLastProperty->pNext 				= (HODBCINSTPROPERTY)malloc( sizeof(ODBCINSTPROPERTY) );
	hLastProperty 						= hLastProperty->pNext;
	memset( hLastProperty, 0, sizeof(ODBCINSTPROPERTY) );
	hLastProperty->nPromptType			= ODBCINST_PROMPTTYPE_LISTBOX;
    hLastProperty->aPromptData			= malloc( sizeof(aYesNo) );
	memcpy( hLastProperty->aPromptData, aYesNo, sizeof(aYesNo) );
	strncpy( hLastProperty->szName, "Trace", INI_MAX_PROPERTY_NAME );
    strcpy( hLastProperty->szValue, "No" );

	hLastProperty->pNext 				= (HODBCINSTPROPERTY)malloc( sizeof(ODBCINSTPROPERTY) );
	hLastProperty 						= hLastProperty->pNext;
	memset( hLastProperty, 0, sizeof(ODBCINSTPROPERTY) );
	hLastProperty->nPromptType			= ODBCINST_PROMPTTYPE_FILENAME;
	strncpy( hLastProperty->szName, "TraceFile", INI_MAX_PROPERTY_NAME );
	strncpy( hLastProperty->szValue, "", INI_MAX_PROPERTY_VALUE );
*/

	/* APPEND OTHERS */
	pODBCINSTGetProperties( hLastProperty );

	return ODBCINST_SUCCESS;
}