File: SQLManageDataSources.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 (188 lines) | stat: -rw-r--r-- 6,296 bytes parent folder | download | duplicates (4)
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
/**************************************************
 * SQLManageDataSources
 *
 **************************************************
 * 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>

/*! 
 * \brief   Get the short name of the UI plugin.
 * 
 *          The short name is the file name without path or file extension.
 *
 *          We silently prepend "lib" here as well.
 *
 * \param   pszName     Place to put short name. Should be FILENAME_MAX bytes. 
 * \param   pszUI       Our generic window handle.
 * 
 * \return  char*       pszName returned for convenience.
 */
char *_getUIPluginName( char *pszName, char *pszUI )
{
    *pszName = '\0';

    /* is it being provided by caller? */
    if ( pszUI && *pszUI )
    {
        sprintf( pszName, "lib%s", pszUI );
        return pszName;
    }

    /* is it being provided by env var? */
    {
        char *pEnvVar = getenv( "ODBCINSTUI" );
        if ( pEnvVar )
        {
            sprintf( pszName, "lib%s", pEnvVar );
            return pszName;
        }
    }

    /* is it being provided by odbcinst.ini? */
    {
        char sz[FILENAME_MAX];
        *sz='\0';
        SQLGetPrivateProfileString( "ODBC", "ODBCINSTUI", "", sz, FILENAME_MAX, "odbcinst.ini" );
        if ( *sz )
        {
            sprintf( pszName, "lib%s", sz );
            return pszName;
        }
    }

    /* default to qt4 */
    strcpy( pszName, "libodbcinstQ4" );

    return pszName;
}

/*! 
 * \brief   Append the file extension used by the OS for plugins.
 * 
 *          We use SHLIBEXT which is picked up at configure/build time.
 *
 * \param   pszNameAndExtension   Output. Needs to be FILENAME_MAX bytes.
 * \param   pszName               Input.
 * 
 * \return  char*   pszNameAndExtension returned for convenience.
 */
char *_appendUIPluginExtension( char *pszNameAndExtension, char *pszName )
{
    if ( strlen( SHLIBEXT ) > 0 )
        sprintf( pszNameAndExtension, "%s%s.1", pszName, SHLIBEXT );
    else
        sprintf( pszNameAndExtension, "%s.so.1", pszName );

    return pszName;
}

/*! 
 * \brief   Prepends the path used for the plugins.
 * 
 *          We use DEFLIB_PATH and if it is not available...
 *          path may not get prepended.
 *
 * \param   pszPathAndName    Output. Needs to be FILENAME_MAX bytes.
 * \param   pszName           Input.
 * 
 * \return  char*   pszPathAndName is returned for convenience.
 */
char *_prependUIPluginPath( char *pszPathAndName, char *pszName )
{
    if ( strlen( DEFLIB_PATH ) > 0 )
        sprintf( pszPathAndName, "%s/%s", DEFLIB_PATH, pszName );
    else
        sprintf( pszPathAndName, "%s", pszName );

    return pszPathAndName;
}

/*! 
 * \brief   UI to manage most ODBC system information.
 * 
 *          This calls into the UI plugin library to do our work for us. The caller can provide
 *          the name (base name) of the library or let us determine which library to use.
 *          See \sa _getUIPluginName for details on how the choice is made.
 *          
 * \param   hWnd    Input. Parent window handle. This is HWND as per the ODBC
 *                  specification but in unixODBC we use a generic window
 *                  handle. Caller must cast a HODBCINSTWND to HWND at call. 
 * 
 * \return  BOOL
 *
 * \sa      ODBCINSTWND
 */
BOOL SQLManageDataSources( HWND hWnd )
{
    HODBCINSTWND    hODBCInstWnd    = (HODBCINSTWND)hWnd;
    char            szName[FILENAME_MAX];
    char            szNameAndExtension[FILENAME_MAX];
    char            szPathAndName[FILENAME_MAX];
	void *          hDLL;
	BOOL	        (*pSQLManageDataSources)(HWND);

    inst_logClear();

    /* ODBC specification states that hWnd is mandatory. */
	if ( !hWnd )
	{
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_INVALID_HWND, "No hWnd" );
		return FALSE;
	}

    /* initialize libtool */
    if ( lt_dlinit() )
    {
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "lt_dlinit() failed" );
		return FALSE;
    }
    lt_dlsetsearchpath(MODULEDIR);

    /* get plugin name */
    _appendUIPluginExtension( szNameAndExtension, _getUIPluginName( szName, hODBCInstWnd->szUI ) );

    /* lets try loading the plugin using an implicit path */
    hDLL = lt_dlopen( szNameAndExtension );
    if ( hDLL )
    {
        /* change the name (SQLManageDataSources to ODBCManageDataSources) to prevent us from calling ourself */
        pSQLManageDataSources = (BOOL (*)(HWND))lt_dlsym( hDLL, "ODBCManageDataSources" );
        if ( pSQLManageDataSources )
            return pSQLManageDataSources( ( *(hODBCInstWnd->szUI) ? hODBCInstWnd->hWnd : NULL ) );
        else
            inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, (char*)lt_dlerror() );
    }
    else
    {
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_WARNING, ODBC_ERROR_GENERAL_ERR, (char*)lt_dlerror() );
        /* try with explicit path */
        _prependUIPluginPath( szPathAndName, szNameAndExtension );
        hDLL = lt_dlopen( szPathAndName );
        if ( hDLL )
        {
            /* change the name (SQLManageDataSources to ODBCManageDataSources) to prevent us from calling ourself   */
            /* its only safe to use hWnd if szUI was specified by the caller                                        */
            pSQLManageDataSources = (BOOL (*)(HWND))lt_dlsym( hDLL, "ODBCManageDataSources" );
            if ( pSQLManageDataSources )
                return pSQLManageDataSources( ( *(hODBCInstWnd->szUI) ? hODBCInstWnd->hWnd : NULL ) );
            else
                inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, (char*)lt_dlerror() );
        }
        else
            inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, (char*)lt_dlerror() );
    }

    /* report failure to caller */
    inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "Failed to load/use a UI plugin." );

    return FALSE;
}