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