1 /**************************************************
2  * odbcinstext.h
3  *
4  **************************************************
5  * This code was created by Peter Harvey @ CodeByDesign.
6  * Released under LGPL 28.JAN.99
7  *
8  * Contributions from...
9  * -----------------------------------------------
10  * Peter Harvey		- pharvey@codebydesign.com
11  **************************************************/
12 #ifndef _ODBCINST_H
13 #define _ODBCINST_H
14 
15 #ifdef UNIXODBC_SOURCE
16 
17 #include <ltdl.h>
18 #ifdef HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif
21 #ifdef HAVE_PWD_H
22 #include <pwd.h>
23 #endif
24 #ifdef HAVE_SYS_TYPES_H
25 #include <sys/types.h>
26 #endif
27 #include <stdio.h>
28 
29 #ifndef ODBCVER
30 #define ODBCVER 0x0380
31 #endif
32 
33 #include <ini.h>
34 #include <log.h>
35 #include <odbcinst.h>
36 
37 /********************************************************
38  * CONSTANTS WHICH DO NOT EXIST ELSEWHERE
39  ********************************************************/
40 #ifndef TRUE
41 #define FALSE 0;
42 #define TRUE 1;
43 #endif
44 
45 #else /* not UNIXODBC_SOURCE */
46 
47 /********************************************************
48   * outside the unixODBC source tree only the            *
49   * public interface is exposed                          *
50   ********************************************************/
51 
52 #include <odbcinst.h>
53 
54 /********************************************************
55   * these limits are originally defined in ini.h         *
56   * but are needed to implement ODBCINSTGetProperties    *
57   * for the Driver Setup                                 *
58   ********************************************************/
59 
60 #define     INI_MAX_LINE            1000
61 #define     INI_MAX_OBJECT_NAME     INI_MAX_LINE
62 #define     INI_MAX_PROPERTY_NAME   INI_MAX_LINE
63 #define     INI_MAX_PROPERTY_VALUE  INI_MAX_LINE
64 
65 #endif  /* UNIXODBC_SOURCE */
66 
67 /********************************************************
68  * PUBLIC API
69  ********************************************************/
70 
71 #ifdef __cplusplus
72 extern "C"
73 {
74 #endif
75 
76 BOOL INSTAPI SQLConfigDataSource(		HWND	hWnd,
77 								WORD	nRequest,
78 								LPCSTR	pszDriver,
79 								LPCSTR	pszAttributes );
80 
81 BOOL INSTAPI SQLGetConfigMode(          UWORD	*pnConfigMode );
82 
83 BOOL INSTAPI SQLGetInstalledDrivers(	LPSTR	pszBuf,
84 								WORD	nBufMax,
85 								WORD	*pnBufOut );
86 
87 BOOL INSTAPI SQLInstallDriverEx(		LPCSTR	pszDriver,
88 								LPCSTR	pszPathIn,
89 								LPSTR	pszPathOut,
90 								WORD	nPathOutMax,
91 								WORD	*nPathOut,
92 								WORD	nRequest,
93 								LPDWORD	pnUsageCount );
94 
95 BOOL INSTAPI SQLInstallDriverManager(	LPSTR	pszPath,
96 								WORD	nPathMax,
97 								WORD	*pnPathOut );
98 
99 RETCODE INSTAPI SQLInstallerError(		WORD	nError,
100 								DWORD	*pnErrorCode,
101 								LPSTR	pszErrorMsg,
102 								WORD	nErrorMsgMax,
103 								WORD	*nErrorMsg );
104 
105 BOOL INSTAPI SQLManageDataSources(		HWND	hWnd );
106 
107 BOOL INSTAPI SQLReadFileDSN(			LPCSTR	pszFileName,
108 								LPCSTR	pszAppName,
109 								LPCSTR	pszKeyName,
110 								LPSTR	pszString,
111 								WORD	nString,
112 								WORD	*pnString );
113 
114 BOOL INSTAPI SQLRemoveDriver(			LPCSTR	pszDriver,
115 								BOOL	nRemoveDSN,
116 								LPDWORD	pnUsageCount );
117 
118 BOOL INSTAPI SQLRemoveDriverManager(	LPDWORD	pnUsageCount );
119 
120 BOOL INSTAPI SQLRemoveDSNFromIni(		LPCSTR	pszDSN );
121 
122 BOOL INSTAPI SQLRemoveTranslator(		LPCSTR	pszTranslator,
123 								LPDWORD	pnUsageCount );
124 
125 BOOL INSTAPI SQLSetConfigMode(			UWORD	nConfigMode );
126 
127 BOOL INSTAPI SQLValidDSN(				LPCSTR	pszDSN );
128 
129 BOOL INSTAPI SQLWriteDSNToIni(			LPCSTR	pszDSN,
130 								LPCSTR	pszDriver );
131 
132 BOOL INSTAPI SQLWriteFileDSN(			LPCSTR	pszFileName,
133 								LPCSTR	pszAppName,
134 								LPCSTR	pszKeyName,
135 								LPCSTR	pszString );
136 
137 BOOL INSTAPI SQLWritePrivateProfileString(
138 								LPCSTR	pszSection,
139 								LPCSTR	pszEntry,
140 								LPCSTR	pszString,
141 								LPCSTR	pszFileName );
142 
143 
144 
145 #ifdef __cplusplus
146 }
147 #endif
148 
149 #ifdef UNIXODBC_SOURCE
150 
151 /********************************************************
152  * PRIVATE API
153  ********************************************************/
154 #if defined(__cplusplus)
155          extern  "C" {
156 #endif
157 
158 BOOL _odbcinst_UserINI(
159 	char *pszFileName,
160 	BOOL bVerify );
161 
162 BOOL _odbcinst_SystemINI(
163 	char *pszFileName,
164 	BOOL bVerify );
165 
166 BOOL _odbcinst_FileINI(	char *pszPath );
167 
168 char * INSTAPI odbcinst_system_file_path( char *buffer );
169 char * INSTAPI odbcinst_system_file_name( char *buffer );
170 char * INSTAPI odbcinst_user_file_path( char *buffer );
171 char * INSTAPI odbcinst_user_file_name( char *buffer );
172 
173 BOOL _odbcinst_ConfigModeINI(
174 	char *pszFileName );
175 
176 int _odbcinst_GetSections(
177 	HINI	hIni,
178 	LPSTR	pRetBuffer,
179 	int		nRetBuffer,
180 	int		*pnBufPos );
181 
182 int _odbcinst_GetEntries(
183 	HINI	hIni,
184 	LPCSTR	pszSection,
185 	LPSTR	pRetBuffer,
186 	int		nRetBuffer,
187 	int		*pnBufPos );
188 
189 int _SQLGetInstalledDrivers(
190 	LPCSTR	pszSection,
191 	LPCSTR	pszEntry,
192 	LPCSTR	pszDefault,
193 	LPCSTR	pRetBuffer,
194 	int     nRetBuffer );
195 
196 BOOL _SQLWriteInstalledDrivers(
197 	LPCSTR	pszSection,
198 	LPCSTR	pszEntry,
199 	LPCSTR	pszString );
200 
201 BOOL _SQLDriverConnectPrompt(
202 	HWND hwnd,
203 	SQLCHAR *dsn,
204 	SQLSMALLINT len_dsn );
205 
206 BOOL _SQLDriverConnectPromptW(
207 	HWND hwnd,
208 	SQLWCHAR *dsn,
209 	SQLSMALLINT len_dsn );
210 
211 void __set_config_mode( int mode );
212 int __get_config_mode( void );
213 
214 int inst_logPushMsg(
215         char *pszModule,
216         char *pszFunctionName,
217         int nLine,
218         int nSeverity,
219         int nCode,
220         char *pszMessage );
221 
222 int inst_logPeekMsg( long nMsg, HLOGMSG *phMsg );
223 int inst_logClear();
224 
225 
226 /*
227  * we should look at caching this info, the calls can become expensive
228  */
229 
230 #ifndef DISABLE_INI_CACHING
231 
232 struct ini_cache
233 {
234     char                *fname;
235     char                *section;
236     char                *entry;
237     char                *value;
238     char                *default_value;
239     int                 buffer_size;
240     int                 ret_value;
241     int                 config_mode;
242     long                timestamp;
243     struct ini_cache    *next;
244 };
245 
246 #endif
247 
248 #ifdef __cplusplus
249 }
250 #endif
251 
252 #endif /* UNIXODBC_SOURCE */
253 
254 /*********************************
255  * ODBCINST - PROPERTIES
256  *********************************
257  *
258  * PURPOSE:
259  *
260  * To provide the caller a mechanism to interact with Data Source properties
261  * containing Driver specific options while avoiding embedding GUI code in
262  * the ODBC infrastructure.
263  *
264  * DETAILS:
265  *
266  *	1.	Application calls libodbcinst.ODBCINSTConstructProperties()
267  *		- odbcinst will load the driver and call libMyDrvS.ODBCINSTGetProperties() to build a list of all possible properties
268  *	2.	Application calls libodbcinst.ODBCINSTSetProperty()
269  *		- use, as required, to init values (ie if configuring existing DataSource)
270  *		- use libodbcinst.SetConfigMode() & libodbcinst.SQLGetPrivateProfileString() to read existing Data Source info (do not forget to set the mode back)
271  *		- do not forget to set mode back to ODBC_BOTH_DSN using SetConfigMode() when done reading
272  *		- no call to Driver Setup
273  *	3.	Application calls libodbcinst.ODBCINSTValidateProperty()
274  *		- use as required (ie on leave widget event)
275  *		- an assesment of the entire property list is also done
276  *		- this is passed onto the driver setup DLL
277  *	4.	Application should refresh widgets in case aPromptData or szValue has changed
278  *		- refresh should occur for each property where bRefresh = 1
279  *	5.	Application calls libodbcinst.ODBCINSTValidateProperties()
280  *		- call this just before saving new Data Source or updating existing Data Source
281  *		- should always call this before saving
282  *		- use libodbcinst.SetConfigMode() & libodbcinst.SQLWritePrivateProfileString() to save Data Source info
283  *		- do not forget to set mode back to ODBC_BOTH_DSN using SetConfigMode() when done saving
284  *		- this is passed onto the driver setup DLL
285  *	6.	Application calls ODBCINSTDestructProperties() to free up memory
286  *		- unload Driver Setup DLL
287  *		- frees memory (Driver Setup allocates most of the memory but we free ALL of it in odbcinst)
288  *
289  * NOTES
290  *
291  *	1.	odbcinst implements 5 functions to support this GUI config stuff
292  *	2.	Driver Setup DLL implements just 3 functions for its share of the work
293  *
294  *********************************/
295 
296 #define ODBCINST_SUCCESS				0
297 #define ODBCINST_WARNING				1
298 #define ODBCINST_ERROR					2
299 
300 #define	ODBCINST_PROMPTTYPE_LABEL		0 /* readonly */
301 #define	ODBCINST_PROMPTTYPE_TEXTEDIT	1
302 #define	ODBCINST_PROMPTTYPE_LISTBOX		2
303 #define	ODBCINST_PROMPTTYPE_COMBOBOX	3
304 #define	ODBCINST_PROMPTTYPE_FILENAME	4
305 #define	ODBCINST_PROMPTTYPE_HIDDEN	    5
306 #define ODBCINST_PROMPTTYPE_TEXTEDIT_PASSWORD 6
307 
308 typedef struct	tODBCINSTPROPERTY
309 {
310 	struct tODBCINSTPROPERTY *pNext;				/* pointer to next property, NULL if last property										*/
311 
312 	char	szName[INI_MAX_PROPERTY_NAME+1];		/* property name																		*/
313 	char	szValue[INI_MAX_PROPERTY_VALUE+1];		/* property value																		*/
314 	int		nPromptType;							/* PROMPTTYPE_TEXTEDIT, PROMPTTYPE_LISTBOX, PROMPTTYPE_COMBOBOX, PROMPTTYPE_FILENAME	*/
315 	char	**aPromptData;							/* array of pointers terminated with a NULL value in array. 							*/
316 	char	*pszHelp;								/* help on this property (driver setups should keep it short)							*/
317 	void	*pWidget;								/* CALLER CAN STORE A POINTER TO ? HERE													*/
318 	int		bRefresh;								/* app should refresh widget ie Driver Setup has changed aPromptData or szValue   		*/
319 	void 	*hDLL;									/* for odbcinst internal use... only first property has valid one 						*/
320 } ODBCINSTPROPERTY, *HODBCINSTPROPERTY;
321 
322 /*
323  * Plugin name
324  */
325 
326 #define ODBCINSTPLUGIN          "odbcinstQ5"
327 
328 /*
329  * Conversion routines for wide interface
330  */
331 
332 char* _multi_string_alloc_and_copy( LPCWSTR in );
333 char* _single_string_alloc_and_copy( LPCWSTR in );
334 void _single_string_copy_to_wide( SQLWCHAR *out, LPCSTR in, int len );
335 void _multi_string_copy_to_wide( SQLWCHAR *out, LPCSTR in, int len );
336 void _single_copy_to_wide( SQLWCHAR *out, LPCSTR in, int len );
337 SQLWCHAR* _multi_string_alloc_and_expand( LPCSTR in );
338 SQLWCHAR* _single_string_alloc_and_expand( LPCSTR in );
339 void _single_copy_from_wide( SQLCHAR *out, LPCWSTR in, int len );
340 int _multi_string_length( LPCSTR in );
341 
342 /*
343  * To support finding UI plugin
344  */
345 char *_getUIPluginName( char *pszName, char *pszUI );
346 char *_appendUIPluginExtension( char *pszNameAndExtension, char *pszName );
347 char *_prependUIPluginPath( char *pszPathAndName, char *pszName );
348 
349 #if defined(__cplusplus)
350          extern  "C" {
351 #endif
352 
353 /* ONLY IMPLEMENTED IN ODBCINST (not in Driver Setup) */
354 int INSTAPI ODBCINSTConstructProperties( char *szDriver, HODBCINSTPROPERTY *hFirstProperty );
355 int INSTAPI ODBCINSTSetProperty( HODBCINSTPROPERTY hFirstProperty, char *pszProperty, char *pszValue );
356 int INSTAPI ODBCINSTDestructProperties( HODBCINSTPROPERTY *hFirstProperty );
357 int INSTAPI ODBCINSTAddProperty( HODBCINSTPROPERTY hFirstProperty, char *pszProperty, char *pszValue );
358 
359 /* IMPLEMENTED IN ODBCINST AND DRIVER SETUP */
360 int INSTAPI ODBCINSTValidateProperty( HODBCINSTPROPERTY hFirstProperty, char *pszProperty, char *pszMessage );
361 int INSTAPI ODBCINSTValidateProperties( HODBCINSTPROPERTY hFirstProperty, HODBCINSTPROPERTY hBadProperty, char *pszMessage );
362 
363 /* ONLY IMPLEMENTED IN DRIVER SETUP (not in ODBCINST) */
364 int INSTAPI ODBCINSTGetProperties( HODBCINSTPROPERTY hFirstProperty );
365 
366 #if defined(__cplusplus)
367          }
368 #endif
369 
370 #endif
371 
372