Fix:Core:Correct parameters for setenv
[navit-package] / navit / main.c
1 /**
2  * Navit, a modular navigation system.
3  * Copyright (C) 2005-2008 Navit Team
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA  02110-1301, USA.
18  */
19
20 #include <locale.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <getopt.h>
24 #include <string.h>
25 #include <signal.h>
26 #include <glib.h>
27 #include <sys/types.h>
28
29 #ifndef _WIN32
30 #include <sys/wait.h>
31 #include <signal.h>
32 #endif
33
34 #include <unistd.h>
35 #include "config.h"
36 #include "file.h"
37 #include "debug.h"
38 #include "main.h"
39 #include "navit.h"
40 #include "gui.h"
41 #include "item.h"
42 #include "xmlconfig.h"
43 #include "coord.h"
44 #include "route.h"
45 #include "navigation.h"
46 #include "event.h"
47 #include "callback.h"
48 #include "navit_nls.h"
49 #if HAVE_API_WIN32_BASE
50 #include <windows.h>
51 #include <winbase.h>
52 #endif
53
54
55 struct map_data *map_data_default;
56
57 struct callback_list *cbl;
58
59
60 static void sigchld(int sig)
61 {
62 #if !defined(_WIN32) && !defined(__CEGCC__)
63         int status;
64         while (waitpid(-1, &status, WNOHANG) > 0);
65 #endif
66 }
67
68 static GList *navit;
69
70 struct iter {
71         GList *list;
72 };
73
74 struct iter *
75 main_iter_new(void)
76 {
77         struct iter *ret=g_new0(struct iter, 1);
78         ret->list=navit;
79         return ret;
80 }
81
82 void
83 main_iter_destroy(struct iter *iter)
84 {
85         g_free(iter);
86 }
87
88 struct navit *
89 main_get_navit(struct iter *iter)
90 {
91         GList *list;
92         struct navit *ret=NULL;
93         if (iter)
94                 list=iter->list;
95         else
96                 list=navit;
97         if (list) {
98                 ret=(struct navit *)(list->data);
99                 if (iter)
100                         iter->list=g_list_next(iter->list);
101         }
102         return ret;
103
104 }
105 void
106 main_add_navit(struct navit *nav)
107 {
108         navit=g_list_prepend(navit, nav);
109         callback_list_call_2(cbl, nav, 1);
110 }
111
112 void
113 main_remove_navit(struct navit *nav)
114 {
115         navit=g_list_remove(navit, nav);
116         callback_list_call_2(cbl, nav, 0);
117         if (! navit)
118                 event_main_loop_quit();
119 }
120
121 int
122 main_add_attr(struct attr *attr)
123 {
124         switch (attr->type)
125         {
126         case attr_callback:
127                 callback_list_add(cbl, attr->u.callback);
128                 return 1;
129         default:
130                 return 0;
131         }
132 }
133
134 int
135 main_remove_attr(struct attr *attr)
136 {
137         switch (attr->type)
138         {
139         case attr_callback:
140                 callback_list_remove(cbl, attr->u.callback);
141                 return 1;
142         default:
143                 return 0;
144         }
145 }
146
147
148 #ifdef HAVE_API_WIN32
149 void
150 setenv(char *var, char *val, int overwrite)
151 {
152         char *str=g_strdup_printf("%s=%s",var,val);
153         if (overwrite || !getenv(var))
154                 putenv(str);
155         g_free(str);
156 }
157 #endif
158
159 /*
160  * environment_vars[][0:name,1-3:mode]
161  * ':'  replaced with NAVIT_PREFIX
162  * '::' replaced with NAVIT_PREFIX and LIBDIR
163  * '~'  replaced with HOME
164 */
165 static char *environment_vars[][5]={
166         {"NAVIT_LIBDIR",      ":",          "::/navit",      ":\\lib",      ":/lib"},
167         {"NAVIT_SHAREDIR",    ":",          ":/share/navit", ":",           ":/share"},
168         {"NAVIT_LOCALEDIR",   ":/../locale",":/share/locale",":\\locale",   ":/locale"},
169         {"NAVIT_USER_DATADIR",":",          "~/.navit",      ":\\data",     ":/home"},
170 #if 1
171         {"NAVIT_LOGFILE",     NULL,         NULL,            ":\\navit.log",NULL},
172 #endif
173         {"NAVIT_LIBPREFIX",   "*/.libs/",   NULL,            NULL,          NULL},
174         {NULL,                NULL,         NULL,            NULL,          NULL},
175 };
176
177 static void
178 main_setup_environment(int mode)
179 {
180         int i=0;
181         char *var,*val,*homedir;
182         while ((var=environment_vars[i][0])) {
183                 val=environment_vars[i][mode+1];
184                 if (val) {
185                         switch (val[0]) {
186                         case ':':
187                                 if (val[1] == ':')
188                                         val=g_strdup_printf("%s/%s%s", getenv("NAVIT_PREFIX"), LIBDIR+sizeof(PREFIX), val+2);
189                                 else
190                                         val=g_strdup_printf("%s%s", getenv("NAVIT_PREFIX"), val+1);
191                                 break;
192                         case '~':
193                                 homedir=getenv("HOME");
194                                 if (!homedir)
195                                         homedir="./";
196                                 val=g_strdup_printf("%s%s", homedir, val+1);
197                                 break;
198                         default:
199                                 val=g_strdup(val);
200                                 break;
201                         }
202                         setenv(var, val, 0);
203                         g_free(val);
204                 }
205                 i++;
206         }
207 }
208
209 #ifdef HAVE_API_WIN32_BASE
210 char *nls_table[][3]={
211         {"DEU","DEU","de_DE"},
212         {"DEA","AUT","de_AT"},
213         {NULL,NULL,NULL},
214 };
215
216 static void
217 win_set_nls(void)
218 {
219         wchar_t wcountry[32],wlang[32]; 
220         char country[32],lang[32];
221         int i=0;
222
223         GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME, wlang, sizeof(wlang));
224         WideCharToMultiByte(CP_ACP,0,wlang,-1,lang,sizeof(lang),NULL,NULL);
225         GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVCTRYNAME, wcountry, sizeof(wcountry));
226         WideCharToMultiByte(CP_ACP,0,wcountry,-1,country,sizeof(country),NULL,NULL);
227         while (nls_table[i][0]) {
228                 if (!strcmp(nls_table[i][0], lang) && !(strcmp(nls_table[i][1], country))) {
229                         dbg(1,"Setting LANG=%s for Lang %s Country %s\n",nls_table[i][2], lang, country);
230                         setenv("LANG",nls_table[i][2],0);
231                         return;
232                 }
233                 i++;
234         }
235         dbg(1,"Lang %s Country %s not found\n",lang,country);
236 }
237 #endif
238
239 void
240 main_init(char *program)
241 {
242         char *s;
243         int l;
244
245 #ifndef _WIN32
246         signal(SIGCHLD, sigchld);
247 #endif
248         cbl=callback_list_new();
249 #ifdef HAVE_API_WIN32_BASE
250         win_set_nls();
251 #endif
252         setenv("LC_NUMERIC","C",1);
253         setlocale(LC_ALL,"");
254         setlocale(LC_NUMERIC,"C");
255 #if !defined _WIN32 && !defined _WIN32_WCE
256         if (file_exists("navit.c") || file_exists("navit.o") || file_exists("navit.lo")) {
257                 char buffer[PATH_MAX];
258                 printf(_("Running from source directory\n"));
259                 getcwd(buffer, PATH_MAX);               /* libc of navit returns "dummy" */
260                 setenv("NAVIT_PREFIX", buffer, 0);
261                 main_setup_environment(0);
262         } else {
263                 if (!getenv("NAVIT_PREFIX")) {
264                 int progpath_len;
265                         char *progpath="/bin/navit";
266                         l=strlen(program);
267                         progpath_len=strlen(progpath);
268                         if (l > progpath_len && !strcmp(program+l-progpath_len,progpath)) {
269                                 s=g_strdup(program);
270                                 s[l-progpath_len]='\0';
271                                 if (strcmp(s, PREFIX))
272                                         printf(_("setting '%s' to '%s'\n"), "NAVIT_PREFIX", s);
273                                 setenv("NAVIT_PREFIX", s, 0);
274                                 g_free(s);
275                         } else
276                                 setenv("NAVIT_PREFIX", PREFIX, 0);
277                 }
278 #ifdef HAVE_API_ANDROID
279                 main_setup_environment(3);
280 #else
281                 main_setup_environment(1);
282 #endif
283         }
284
285 #else           /* _WIN32 || _WIN32_WCE */
286         if (!getenv("NAVIT_PREFIX"))
287         {
288                 char  filename[MAX_PATH + 1],
289                      *end;
290
291                 *filename = '\0';
292 #ifdef _UNICODE         /* currently for wince */
293                 wchar_t wfilename[MAX_PATH + 1];
294                 if (GetModuleFileNameW(NULL, wfilename, MAX_PATH))
295                 {
296                         wcstombs(filename, wfilename, MAX_PATH);
297 #else
298                 if (GetModuleFileName(NULL, filename, MAX_PATH))
299                 {
300 #endif
301                         end = strrchr(filename, L'\\'); /* eliminate the file name which is on the right side */
302                         if(end)
303                                 *end = '\0';
304                 }
305                 setenv("NAVIT_PREFIX", filename, 0);
306         }
307         if (!getenv("HOME"))
308                 setenv("HOME", getenv("NAVIT_PREFIX"), 0);
309         main_setup_environment(2);
310 #endif  /* _WIN32 || _WIN32_WCE */
311
312         if (getenv("LC_ALL"))
313                 dbg(0,"Warning: LC_ALL is set, this might lead to problems (e.g. strange positions from GPS)\n");
314         s = getenv("NAVIT_WID");
315         if (s) {
316                 setenv("SDL_WINDOWID", s, 0);
317         }
318 }
319
320 void
321 main_init_nls(void)
322 {
323 #ifdef ENABLE_NLS
324 #ifdef FORCE_LOCALE
325 #define STRINGIFY2(x) #x
326 #define STRINGIFY(x) STRINGIFY2(x)
327         setlocale(LC_MESSAGES,STRINGIFY(FORCE_LOCALE));
328 #endif
329         bindtextdomain(PACKAGE, getenv("NAVIT_LOCALEDIR"));
330         bind_textdomain_codeset (PACKAGE, "UTF-8");
331         textdomain(PACKAGE);
332 #endif
333 }