Fix:graphics_cocoa:Better main argument handling
[profile/ivi/navit.git] / navit / 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 "config.h"
24 #ifdef HAVE_GETOPT_H
25 #include <getopt.h>
26 #else
27 #include <XGetopt.h>
28 #endif
29 #include "config_.h"
30 #include "version.h"
31 #include "item.h"
32 #include "coord.h"
33 #include "main.h"
34 #include "route.h"
35 #include "navigation.h"
36 #include "track.h"
37 #include "debug.h"
38 #include "event.h"
39 #include "event_glib.h"
40 #include "xmlconfig.h"
41 #include "file.h"
42 #include "search.h"
43 #include "start_real.h"
44 #include "linguistics.h"
45 #include "navit_nls.h"
46 #include "atom.h"
47 #include "command.h"
48 #ifdef HAVE_API_WIN32_CE
49 #include <windows.h>
50 #include <winbase.h>
51 #endif
52
53 char *version=PACKAGE_VERSION" "SVN_VERSION""NAVIT_VARIANT;
54 int main_argc;
55 char **main_argv;
56
57 static void
58 print_usage(void)
59 {
60         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"));
61 }
62
63
64 #ifndef USE_PLUGINS
65 extern void builtin_init(void);
66 #endif /* USE_PLUGINS*/
67
68 int main_real(int argc, char **argv)
69 {
70         xmlerror *error = NULL;
71         char *config_file = NULL, *command=NULL, *startup_file=NULL;
72         int opt;
73         char *cp;
74         struct attr navit, conf;
75
76         GList *list = NULL, *li;
77         main_argc=argc;
78         main_argv=argv;
79
80
81 #ifdef HAVE_GLIB
82         event_glib_init();
83 #else
84         _g_slice_thread_init_nomessage();
85 #endif
86         atom_init();
87         main_init(argv[0]);
88         main_init_nls();
89         debug_init(argv[0]);
90
91         cp = getenv("NAVIT_LOGFILE");
92         if (cp) {
93                 debug_set_logfile(cp);
94         }
95 #ifdef HAVE_API_WIN32_CE
96         else {  
97                 debug_set_logfile("/Storage Card/navit.log");
98         }
99 #endif
100         file_init();
101 #ifndef USE_PLUGINS
102         builtin_init();
103 #endif
104         route_init();
105         navigation_init();
106         tracking_init();
107         search_init();
108         linguistics_init();
109         config_file=NULL;
110 #ifdef HAVE_GETOPT_H
111         opterr=0;  //don't bomb out on errors.
112 #endif /* _MSC_VER */
113         /* ingore iphone command line argument */
114         if (argc == 2 && !strcmp(argv[1],"-RegisterForSystemEvents"))
115                 argc=1;
116         if (argc > 1) {
117                 /* DEVELOPPERS : don't forget to update the manpage if you modify theses options */
118                 while((opt = getopt(argc, argv, ":hvc:d:e:s:")) != -1) {
119                         switch(opt) {
120                         case 'h':
121                                 print_usage();
122                                 exit(0);
123                                 break;
124                         case 'v':
125                                 printf("%s %s\n", "navit", version);
126                                 exit(0);
127                                 break;
128                         case 'c':
129                                 printf("config file n is set to `%s'\n", optarg);
130                     config_file = optarg;
131                                 break;
132                         case 'd':
133                                 printf("TODO Verbose option is set to `%s'\n", optarg);
134                                 break;
135                         case 'e':
136                                 command=optarg;
137                                 break;
138                         case 's':
139                                 startup_file=optarg;
140                                 break;
141 #ifdef HAVE_GETOPT_H
142                         case ':':
143                                 fprintf(stderr, "navit: Error - Option `%c' needs a value\n", optopt);
144                                 print_usage();
145                                 exit(1);
146                                 break;
147                         case '?':
148                                 fprintf(stderr, "navit: Error - No such option: `%c'\n", optopt);
149                                 print_usage();
150                                 exit(1);
151 #endif
152                         }
153           }
154                 // use 1st cmd line option that is left for the config file
155                 if (optind < argc) config_file = argv[optind];
156         }
157
158     // if config file is explicitely given only look for it, otherwise try std paths
159         if (config_file) list = g_list_append(list,g_strdup(config_file));
160     else {
161                 list = g_list_append(list,g_strjoin(NULL,getenv("NAVIT_USER_DATADIR"), "/navit.xml" , NULL));
162                 list = g_list_append(list,g_strdup("navit.xml.local"));
163                 list = g_list_append(list,g_strdup("navit.xml"));
164 #ifdef HAVE_API_ANDROID
165                 // new preferred location (the new one should have priority over the legacy!)
166                 list = g_list_append(list,g_strdup("/sdcard/navit/navit.xml"));
167                 // legacy location, still supported
168                 list = g_list_append(list,g_strdup("/sdcard/navit.xml"));
169 #endif
170                 list = g_list_append(list,g_strjoin(NULL,getenv("NAVIT_SHAREDIR"), "/navit.xml.local" , NULL));
171                 list = g_list_append(list,g_strjoin(NULL,getenv("NAVIT_SHAREDIR"), "/navit.xml" , NULL));
172 #ifndef _WIN32
173                 list = g_list_append(list,g_strdup("/etc/navit/navit.xml"));
174 #endif
175         }
176         li = list;
177         for (;;) {
178                 if (li == NULL) {
179                         // We have not found an existing config file from all possibilities
180                         dbg(0,_("No config file navit.xml, navit.xml.local found\n"));
181                         return 1;
182                 }
183         // Try the next config file possibility from the list
184                 config_file = li->data;
185                 if (file_exists(config_file))
186                 {
187                         break;
188                 }
189                 else
190                         g_free(config_file);
191                 li = g_list_next(li);
192         }
193
194         if (!config_load(config_file, &error)) {
195                 dbg(0, _("Error parsing '%s': %s\n"), config_file, error ? error->message : "");
196         } else {
197                 dbg(0, _("Using '%s'\n"), config_file);
198         }
199         while (li) {
200                 g_free(li->data);
201                 li = g_list_next(li);
202         }
203         g_list_free(list);
204         if (! config_get_attr(config, attr_navit, &navit, NULL) && !config_empty_ok) {
205                 dbg(0, _("No instance has been created, exiting\n"));
206                 exit(1);
207         }
208         conf.type=attr_config;
209         conf.u.config=config;
210         if (startup_file) {
211                 FILE *f=fopen(startup_file,"r");
212                 if (f) {
213                         char buffer[4096];
214                         while(fgets(buffer, sizeof(buffer), f)) {
215                                 command_evaluate(&conf, buffer);
216                         }
217                 }
218         }
219         if (command) {
220                 command_evaluate(&conf, command);
221         }
222         event_main_loop_run();
223
224 #ifndef HAVE_API_ANDROID
225         debug_finished();
226 #endif
227         return 0;
228 }