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