1/*
2 *  main.c
3 *
4 *  $Id: main.c,v 1.9 2007/02/02 11:58:14 source Exp $
5 *
6 *  Main program
7 *
8 *  The iODBC driver manager.
9 *
10 *  Copyright (C) 1996-2006 by OpenLink Software <iodbc@openlinksw.com>
11 *  All Rights Reserved.
12 *
13 *  This software is released under the terms of either of the following
14 *  licenses:
15 *
16 *      - GNU Library General Public License (see LICENSE.LGPL)
17 *      - The BSD License (see LICENSE.BSD).
18 *
19 *  Note that the only valid version of the LGPL license as far as this
20 *  project is concerned is the original GNU Library General Public License
21 *  Version 2, dated June 1991.
22 *
23 *  While not mandated by the BSD license, any patches you make to the
24 *  iODBC source code may be contributed back into the iODBC project
25 *  at your discretion. Contributions will benefit the Open Source and
26 *  Data Access community as a whole. Submissions may be made at:
27 *
28 *      http://www.iodbc.org
29 *
30 *
31 *  GNU Library Generic Public License Version 2
32 *  ============================================
33 *  This library is free software; you can redistribute it and/or
34 *  modify it under the terms of the GNU Library General Public
35 *  License as published by the Free Software Foundation; only
36 *  Version 2 of the License dated June 1991.
37 *
38 *  This library is distributed in the hope that it will be useful,
39 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
40 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
41 *  Library General Public License for more details.
42 *
43 *  You should have received a copy of the GNU Library General Public
44 *  License along with this library; if not, write to the Free
45 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
46 *
47 *
48 *  The BSD License
49 *  ===============
50 *  Redistribution and use in source and binary forms, with or without
51 *  modification, are permitted provided that the following conditions
52 *  are met:
53 *
54 *  1. Redistributions of source code must retain the above copyright
55 *     notice, this list of conditions and the following disclaimer.
56 *  2. Redistributions in binary form must reproduce the above copyright
57 *     notice, this list of conditions and the following disclaimer in
58 *     the documentation and/or other materials provided with the
59 *     distribution.
60 *  3. Neither the name of OpenLink Software Inc. nor the names of its
61 *     contributors may be used to endorse or promote products derived
62 *     from this software without specific prior written permission.
63 *
64 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
65 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
66 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
67 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL OPENLINK OR
68 *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
69 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
70 *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
71 *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
72 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
73 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
74 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
75 */
76
77
78#include <iodbc.h>
79#include <isql.h>
80#include <odbcinst.h>
81#include <unistd.h>
82#include <stdlib.h>
83
84#include "gui.h"
85
86
87int
88gtk_gui (int *argc, char **argv[])
89{
90  GtkWidget *mainwnd;
91#if GTK_CHECK_VERSION(2,0,0)
92  gtk_set_locale();
93#endif
94  gtk_init (argc, argv);
95  mainwnd = gtk_window_new (GTK_WINDOW_TOPLEVEL);
96  return SQLManageDataSources (mainwnd);
97}
98
99
100int
101kde_gui (int *argc, char **argv[])
102{
103  return -1;
104}
105
106
107void
108display_help (void)
109{
110  printf ("-help\t\t\tDisplay the list of options.\n\r");
111  printf
112      ("-odbc filename\t\tSet the location of the user ODBC.INI file.\n\r");
113  printf
114      ("-odbcinst filename\tSet the location of the user ODBCINST.INI file.\n\r");
115  printf
116      ("-sysodbc filename\tSet the location of the system ODBC.INI file.\n\r");
117  printf
118      ("-sysodbcinst filename\tSet the location of the system ODBCINST.INI file.\n\r");
119  printf ("-gui guitype\t\tSet the GUI type : GTK, KDE.\n\r");
120  printf ("-debug\t\t\tThe error messages are displayed on the console.\n\r");
121  printf
122      ("-admin odbcinstfile\tUsed to administrate the system odbcinst.ini file.\n\r\n\r");
123  _exit (1);
124}
125
126
127#if !defined(HAVE_SETENV)
128static int
129setenv (const char *name, const char *value, int overwrite)
130{
131  int rc;
132  char *entry;
133
134  /*
135   *  Allocate some space for new environment variable
136   */
137  if ((entry = (char *) malloc (strlen (name) + strlen (value) + 2)) == NULL)
138    return -1;
139  strcpy (entry, name);
140  strcat (entry, "=");
141  strcat (entry, value);
142
143  /*
144   *  Check if variable already exists in current environment and whether
145   *  we want to overwrite it with a new value if it exists.
146   */
147  if (getenv (name) != NULL && !overwrite)
148    {
149      free (entry);
150      return 0;
151    }
152
153  /*
154   *  Add the variable to the environment.
155   */
156  rc = putenv (entry);
157  free (entry);
158  return (rc == 0) ? 0 : -1;
159}
160#endif /* HAVE_SETENV */
161
162
163int
164main (int argc, char *argv[])
165{
166  BOOL debug = FALSE;
167  char path[4096];
168  char *gui = NULL;
169  int i = 1;
170
171  printf ("iODBC Administrator (GTK)\n");
172  printf ("%s\n", PACKAGE_STRING);
173  printf ("Copyright (C) 2000-2006 OpenLink Software\n");
174  printf ("Please report all bugs to <%s>\n\n", PACKAGE_BUGREPORT);
175
176  /* Check options commands */
177  if (argc > 1)
178    {
179      for (; i < argc; i++)
180	{
181	  if (!strcasecmp (argv[i], "-help"))
182	    display_help ();
183
184	  if (!strcasecmp (argv[i], "-debug"))
185	    debug = TRUE;
186
187	  if (!strcasecmp (argv[i], "-odbc"))
188	    {
189	      if (i + 1 >= argc)
190		display_help ();
191	      setenv ("ODBCINI", argv[++i], TRUE);
192	    }
193
194	  if (!strcasecmp (argv[i], "-admin"))
195	    {
196	      if (i + 1 >= argc)
197		display_help ();
198	      setenv ("ODBCINSTINI", argv[++i], TRUE);
199	      setenv ("SYSODBCINSTINI", argv[i], TRUE);
200	    }
201
202	  if (!strcasecmp (argv[i], "-odbcinst"))
203	    {
204	      if (i + 1 >= argc)
205		display_help ();
206	      setenv ("ODBCINSTINI", argv[++i], TRUE);
207	    }
208
209	  if (!strcasecmp (argv[i], "-sysodbc"))
210	    {
211	      if (i + 1 >= argc)
212		display_help ();
213	      setenv ("SYSODBCINI", argv[++i], TRUE);
214	    }
215
216	  if (!strcasecmp (argv[i], "-sysodbcinst"))
217	    {
218	      if (i + 1 >= argc)
219		display_help ();
220	      setenv ("SYSODBCINSTINI", argv[++i], TRUE);
221	    }
222
223	  if (!strcasecmp (argv[i], "-gui"))
224	    {
225	      if (i + 2 >= argc)
226		display_help ();
227	      gui = argv[++i];
228	    }
229	}
230    }
231
232  if (!getenv ("ODBCINI") && getenv ("HOME"))
233    {
234      STRCPY (path, getenv ("HOME"));
235      STRCAT (path, "/.odbc.ini");
236      setenv ("ODBCINI", path, TRUE);
237    }
238
239  if (!debug)
240    {
241      close (STDOUT_FILENO);
242      close (STDERR_FILENO);
243    }
244
245  if (gui && !strcasecmp (gui, "KDE"))
246    return kde_gui (&argc, &argv);
247
248  return gtk_gui (&argc, &argv);
249}
250