Fix:Core:Better win32 module loading
[navit-package] / navit / start_real.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 <stdio.h>
21 #include <stdlib.h>
22 #include <glib.h>
23 #include <getopt.h>
24 #include "config.h"
25 #include "config_.h"
26 #include "version.h"
27 #include "item.h"
28 #include "coord.h"
29 #include "main.h"
30 #include "route.h"
31 #include "navigation.h"
32 #include "track.h"
33 #include "debug.h"
34 #include "event.h"
35 #include "event_glib.h"
36 #include "xmlconfig.h"
37 #include "file.h"
38 #include "search.h"
39 #include "start_real.h"
40 #include "linguistics.h"
41 #include "navit_nls.h"
42 #include "atom.h"
43 #ifdef HAVE_API_WIN32_CE
44 #include <windows.h>
45 #include <winbase.h>
46 #endif
47
48 char *version=PACKAGE_VERSION" "SVN_VERSION""NAVIT_VARIANT; 
49 int main_argc;
50 char **main_argv;
51
52 static void
53 print_usage(void)
54 {
55         printf(_("navit usage:\nnavit [options] [configfile]\n\t-c <file>: use <file> as config file\n\t-d <n>: set the debug output level to <n>. (TODO)\n\t-h: print this usage info and exit.\n\t-v: Print the version and exit.\n"));
56 }
57
58
59 int main_real(int argc, char **argv)
60 {
61         xmlerror *error = NULL;
62         char *config_file = NULL;
63         int opt;
64         char *cp;
65         struct attr navit;
66
67         GList *list = NULL, *li;
68         main_argc=argc;
69         main_argv=argv;
70
71
72 #ifdef HAVE_GLIB
73         event_glib_init();
74 #endif
75         atom_init();
76         main_init(argv[0]);
77         main_init_nls();
78         debug_init(argv[0]);
79         
80         cp = getenv("NAVIT_LOGFILE");
81         if (cp)
82                 debug_set_logfile(cp);
83 #ifdef HAVE_API_WIN32_CE
84         else
85                 debug_set_logfile("/Storage Card/navit.log");
86 #endif
87         file_init();
88 #ifndef USE_PLUGINS
89         extern void builtin_init(void);
90         builtin_init();
91 #endif
92         route_init();
93         navigation_init();
94         tracking_init();
95         search_init();
96         linguistics_init();
97         config_file=NULL;
98         opterr=0;  //don't bomb out on errors.
99         if (argc > 1) {
100                 /* DEVELOPPERS : don't forget to update the manpage if you modify theses options */
101                 while((opt = getopt(argc, argv, ":hvc:d:")) != -1) {
102                         switch(opt) {
103                         case 'h':
104                                 print_usage();
105                                 exit(0);
106                                 break;
107                         case 'v':
108                                 printf("%s %s\n", "navit", version); 
109                                 exit(0);
110                                 break;
111                         case 'c':
112                                 printf("config file n is set to `%s'\n", optarg);
113                     config_file = optarg;
114                                 break;
115                         case 'd':
116                                 printf("TODO Verbose option is set to `%s'\n", optarg);
117                                 break;
118                         case ':':
119                                 fprintf(stderr, "navit: Error - Option `%c' needs a value\n", optopt);
120                                 print_usage();
121                                 exit(1);
122                                 break;
123                         case '?':
124                                 fprintf(stderr, "navit: Error - No such option: `%c'\n", optopt);
125                                 print_usage();
126                                 exit(1);
127                         }
128             }
129         }
130         // use 1st cmd line option that is left for the config file
131         if (optind < argc) config_file = argv[optind];
132
133     // if config file is explicitely given only look for it, otherwise try std paths
134         if (config_file) list = g_list_append(list,g_strdup(config_file));
135     else {
136                 list = g_list_append(list,g_strjoin(NULL,getenv("NAVIT_USER_DATADIR"), "/navit.xml" , NULL));
137                 list = g_list_append(list,g_strdup("navit.xml.local"));
138                 list = g_list_append(list,g_strdup("navit.xml"));
139 #ifdef HAVE_API_ANDROID
140                 list = g_list_append(list,g_strdup("/sdcard/navit.xml"));
141 #endif
142                 list = g_list_append(list,g_strjoin(NULL,getenv("NAVIT_SHAREDIR"), "/navit.xml.local" , NULL));
143                 list = g_list_append(list,g_strjoin(NULL,getenv("NAVIT_SHAREDIR"), "/navit.xml" , NULL));
144 #ifndef _WIN32
145                 list = g_list_append(list,g_strdup("/etc/navit/navit.xml"));
146 #endif
147         }
148         li = list;
149         for (;;) {
150                 if (li == NULL) {
151                         // We have not found an existing config file from all possibilities
152                         dbg(0,_("No config file navit.xml, navit.xml.local found\n"));
153                         return 1;
154                 }
155         // Try the next config file possibility from the list
156                 config_file = li->data;
157                 if (file_exists(config_file))
158                         break;
159                 else
160                         g_free(config_file);
161                 li = g_list_next(li);
162         }
163
164         if (!config_load(config_file, &error)) {
165                 dbg(0, _("Error parsing '%s': %s\n"), config_file, error ? error->message : "");
166         } else {
167                 dbg(0, _("Using '%s'\n"), config_file);
168         }
169         while (li) {
170                 g_free(li->data);
171                 li = g_list_next(li);
172         }
173         g_list_free(list);
174         if (! config_get_attr(config, attr_navit, &navit, NULL) && !config_empty_ok) {
175                 dbg(0, _("No instance has been created, exiting\n"));
176                 exit(1);
177         }
178         event_main_loop_run();
179
180         return 0;
181 }