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