Fix:Various:Fixed some warnings
[navit-package] / navit / start.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
40
41 static void
42 print_usage(void)
43 {
44         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"));
45 }
46
47
48 static gchar *
49 get_home_directory(void)
50 {
51         static gchar *homedir = NULL;
52
53         if (homedir) return homedir;
54         homedir = getenv("HOME");
55         if (!homedir)
56         {
57                 dbg(0,"Could not find home directory. Using current directory as home directory.");
58 #ifdef _WIN32_WCE
59                 homedir = "/Storage Card/";
60 #else
61                 homedir = ".";
62 #endif
63         } else {
64                 homedir=g_strdup(homedir);
65         }
66         return homedir;
67 }
68
69 int main(int argc, char **argv)
70 {
71         GError *error = NULL;
72         char *config_file = NULL;
73         int opt;
74
75     GList *list = NULL, *li;
76
77
78 #ifdef HAVE_GLIB
79         event_glib_init();
80 #endif
81         main_init(argv[0]);
82         main_init_nls();
83         debug_init(argv[0]);
84 #ifdef __CEGCC__
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         config_file=NULL;
97         opterr=0;  //don't bomb out on errors.
98         if (argc > 1) {
99                 /* DEVELOPPERS : don't forget to update the manpage if you modify theses options */
100                 while((opt = getopt(argc, argv, ":hvc:d:")) != -1) {
101                         switch(opt) {
102                         case 'h':
103                                 print_usage();
104                                 exit(0);
105                                 break;
106                         case 'v':
107                                 printf("%s %s\n", "navit", PACKAGE_VERSION" "SVN_VERSION); 
108                                 exit(0);
109                                 break;
110                         case 'c':
111                                 printf("config file n is set to `%s'\n", optarg);
112                     config_file = optarg;
113                                 break;
114                         case 'd':
115                                 printf("TODO Verbose option is set to `%s'\n", optarg);
116                                 break;
117                         case ':':
118                                 fprintf(stderr, "navit: Error - Option `%c' needs a value\n", optopt);
119                                 print_usage();
120                                 exit(1);
121                                 break;
122                         case '?':
123                                 fprintf(stderr, "navit: Error - No such option: `%c'\n", optopt);
124                                 print_usage();
125                                 exit(1);
126                         }
127             }
128         }
129         // use 1st cmd line option that is left for the config file
130         if (optind < argc) config_file = argv[optind];
131
132     // if config file is explicitely given only look for it, otherwise try std paths
133         if (config_file) list = g_list_append(list,g_strdup(config_file));
134     else {
135                 list = g_list_append(list,g_strjoin(NULL,get_home_directory(), "/.navit/navit.xml" , NULL));
136                 list = g_list_append(list,g_strdup("navit.xml.local"));
137                 list = g_list_append(list,g_strdup("navit.xml"));
138                 list = g_list_append(list,g_strjoin(NULL,getenv("NAVIT_SHAREDIR"), "/navit.xml.local" , NULL));
139                 list = g_list_append(list,g_strjoin(NULL,getenv("NAVIT_SHAREDIR"), "/navit.xml" , NULL));
140                 #ifndef _WIN32
141                 list = g_list_append(list,g_strdup("/etc/navit/navit.xml"));
142                 #endif
143         }
144         li = list;
145         do {
146                 if (li == NULL) {
147                         // We have not found an existing config file from all possibilities
148                         printf(_("No config file navit.xml, navit.xml.local found\n"));
149                         exit(1);
150                 }
151         // Try the next config file possibility from the list
152                 config_file = li->data;
153                 if (!file_exists(config_file)) g_free(config_file);
154                 li = g_list_next(li);
155         } while (!file_exists(config_file));
156         g_list_free(list);
157
158 #ifdef HAVE_GLIB
159         event_request_system("glib","start");
160 #endif
161 #ifdef HAVE_API_WIN32_CE
162         config_file="\\Storage Card\\navit.xml";
163 #endif
164         if (!config_load(config_file, &error)) {
165                 printf(_("Error parsing '%s': %s\n"), config_file, error ? error->message : "");
166                 exit(1);
167         } else {
168                 printf(_("Using '%s'\n"), config_file);
169         }
170         if (! main_get_navit(NULL)) {
171                 printf(_("No instance has been created, exiting\n"));
172                 exit(1);
173         }
174         event_main_loop_run();
175
176         return 0;
177 }