]> git.mxchange.org Git - flightgear.git/blob - src/Main/options.cxx
Merge branch 'next' of git://gitorious.org/fg/flightgear into next
[flightgear.git] / src / Main / options.cxx
1 // options.cxx -- class to handle command line options
2 //
3 // Written by Curtis Olson, started April 1998.
4 //
5 // Copyright (C) 1998  Curtis L. Olson  - http://www.flightgear.org/~curt
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <simgear/compiler.h>
29 #include <simgear/structure/exception.hxx>
30 #include <simgear/debug/logstream.hxx>
31 #include <simgear/timing/sg_time.hxx>
32 #include <simgear/misc/sg_dir.hxx>
33
34 #include <math.h>               // rint()
35 #include <stdio.h>
36 #include <stdlib.h>             // atof(), atoi()
37 #include <string.h>             // strcmp()
38 #include <algorithm>
39
40 #include <iostream>
41 #include <string>
42
43 #include <simgear/math/sg_random.h>
44 #include <simgear/props/props_io.hxx>
45 #include <simgear/misc/sgstream.hxx>
46 #include <simgear/misc/sg_path.hxx>
47 #include <simgear/scene/material/mat.hxx>
48 #include <simgear/sound/soundmgr_openal.hxx>
49 #include <Autopilot/route_mgr.hxx>
50 #include <GUI/gui.h>
51
52 #include "globals.hxx"
53 #include "fg_init.hxx"
54 #include "fg_props.hxx"
55 #include "options.hxx"
56 #include "util.hxx"
57 #include "viewmgr.hxx"
58 #include <Main/viewer.hxx>
59
60 #include <simgear/version.h>
61 #include <osg/Version>
62
63 using std::string;
64 using std::sort;
65 using std::cout;
66 using std::cerr;
67 using std::endl;
68
69 #if defined( HAVE_VERSION_H ) && HAVE_VERSION_H
70 #  include <Include/version.h>
71 #else
72 #  include <Include/no_version.h>
73 #endif
74
75 #define NEW_DEFAULT_MODEL_HZ 120
76
77 enum
78 {
79     FG_OPTIONS_OK = 0,
80     FG_OPTIONS_HELP = 1,
81     FG_OPTIONS_ERROR = 2,
82     FG_OPTIONS_EXIT = 3,
83     FG_OPTIONS_VERBOSE_HELP = 4,
84     FG_OPTIONS_SHOW_AIRCRAFT = 5,
85     FG_OPTIONS_SHOW_SOUND_DEVICES = 6
86 };
87
88 static double
89 atof( const string& str )
90 {
91     return ::atof( str.c_str() );
92 }
93
94 static int
95 atoi( const string& str )
96 {
97     return ::atoi( str.c_str() );
98 }
99
100 /**
101  * Set a few fail-safe default property values.
102  *
103  * These should all be set in $FG_ROOT/preferences.xml, but just
104  * in case, we provide some initial sane values here. This method
105  * should be invoked *before* reading any init files.
106  */
107 void
108 fgSetDefaults ()
109 {
110     // set a possibly independent location for scenery data
111     const char *envp = ::getenv( "FG_SCENERY" );
112
113     if ( envp != NULL ) {
114         // fg_root could be anywhere, so default to environmental
115         // variable $FG_ROOT if it is set.
116         globals->set_fg_scenery(envp);
117     } else {
118         // Otherwise, default to Scenery being in $FG_ROOT/Scenery
119         globals->set_fg_scenery("");
120     }
121         
122                                 // Position (deliberately out of range)
123     fgSetDouble("/position/longitude-deg", 9999.0);
124     fgSetDouble("/position/latitude-deg", 9999.0);
125     fgSetDouble("/position/altitude-ft", -9999.0);
126
127                                 // Orientation
128     fgSetDouble("/orientation/heading-deg", 9999.0);
129     fgSetDouble("/orientation/roll-deg", 0.0);
130     fgSetDouble("/orientation/pitch-deg", 0.424);
131
132                                 // Velocities
133     fgSetDouble("/velocities/uBody-fps", 0.0);
134     fgSetDouble("/velocities/vBody-fps", 0.0);
135     fgSetDouble("/velocities/wBody-fps", 0.0);
136     fgSetDouble("/velocities/speed-north-fps", 0.0);
137     fgSetDouble("/velocities/speed-east-fps", 0.0);
138     fgSetDouble("/velocities/speed-down-fps", 0.0);
139     fgSetDouble("/velocities/airspeed-kt", 0.0);
140     fgSetDouble("/velocities/mach", 0.0);
141
142                                 // Presets
143     fgSetDouble("/sim/presets/longitude-deg", 9999.0);
144     fgSetDouble("/sim/presets/latitude-deg", 9999.0);
145     fgSetDouble("/sim/presets/altitude-ft", -9999.0);
146
147     fgSetDouble("/sim/presets/heading-deg", 9999.0);
148     fgSetDouble("/sim/presets/roll-deg", 0.0);
149     fgSetDouble("/sim/presets/pitch-deg", 0.424);
150
151     fgSetString("/sim/presets/speed-set", "knots");
152     fgSetDouble("/sim/presets/airspeed-kt", 0.0);
153     fgSetDouble("/sim/presets/mach", 0.0);
154     fgSetDouble("/sim/presets/uBody-fps", 0.0);
155     fgSetDouble("/sim/presets/vBody-fps", 0.0);
156     fgSetDouble("/sim/presets/wBody-fps", 0.0);
157     fgSetDouble("/sim/presets/speed-north-fps", 0.0);
158     fgSetDouble("/sim/presets/speed-east-fps", 0.0);
159     fgSetDouble("/sim/presets/speed-down-fps", 0.0);
160
161     fgSetBool("/sim/presets/onground", true);
162     fgSetBool("/sim/presets/trim", false);
163
164                                 // Miscellaneous
165     fgSetBool("/sim/startup/game-mode", false);
166     fgSetBool("/sim/startup/splash-screen", true);
167     fgSetBool("/sim/startup/intro-music", true);
168     // we want mouse-pointer to have an undefined value if nothing is
169     // specified so we can do the right thing for voodoo-1/2 cards.
170     // fgSetString("/sim/startup/mouse-pointer", "disabled");
171     fgSetString("/sim/control-mode", "joystick");
172     fgSetBool("/sim/auto-coordination", false);
173 #if defined(WIN32)
174     fgSetString("/sim/startup/browser-app", "webrun.bat");
175 #elif defined(__APPLE__)
176     fgSetString("/sim/startup/browser-app", "open");
177 #elif defined(sgi)
178     fgSetString("/sim/startup/browser-app", "launchWebJumper");
179 #else
180     envp = ::getenv( "WEBBROWSER" );
181     if (!envp) envp = "netscape";
182     fgSetString("/sim/startup/browser-app", envp);
183 #endif
184     fgSetString("/sim/logging/priority", "alert");
185
186                                 // Features
187     fgSetBool("/sim/hud/color/antialiased", false);
188     fgSetBool("/sim/hud/enable3d", true);
189     fgSetBool("/sim/hud/visibility", false);
190     fgSetBool("/sim/panel/visibility", true);
191     fgSetBool("/sim/sound/enabled", true);
192     fgSetBool("/sim/sound/working", true);
193
194                                 // Flight Model options
195     fgSetString("/sim/flight-model", "jsb");
196     fgSetString("/sim/aero", "c172");
197     fgSetInt("/sim/model-hz", NEW_DEFAULT_MODEL_HZ);
198     fgSetInt("/sim/speed-up", 1);
199
200                                 // Rendering options
201     fgSetString("/sim/rendering/fog", "nicest");
202     fgSetBool("/environment/clouds/status", true);
203     fgSetBool("/sim/startup/fullscreen", false);
204     fgSetBool("/sim/rendering/shading", true);
205     fgSetBool("/sim/rendering/skyblend", true);
206     fgSetBool("/sim/rendering/textures", true);
207     fgTie( "/sim/rendering/filtering", SGGetTextureFilter, SGSetTextureFilter, false);
208     fgSetInt("/sim/rendering/filtering", 1);
209     fgSetBool("/sim/rendering/wireframe", false);
210     fgSetBool("/sim/rendering/horizon-effect", false);
211     fgSetBool("/sim/rendering/enhanced-lighting", false);
212     fgSetBool("/sim/rendering/distance-attenuation", false);
213     fgSetBool("/sim/rendering/specular-highlight", true);
214     fgSetInt("/sim/startup/xsize", 800);
215     fgSetInt("/sim/startup/ysize", 600);
216     fgSetInt("/sim/rendering/bits-per-pixel", 16);
217     fgSetString("/sim/view-mode", "pilot");
218     fgSetDouble("/sim/current-view/heading-offset-deg", 0);
219
220                                 // HUD options
221     fgSetString("/sim/startup/units", "feet");
222     fgSetString("/sim/hud/frame-stat-type", "tris");
223         
224                                 // Time options
225     fgSetInt("/sim/startup/time-offset", 0);
226     fgSetString("/sim/startup/time-offset-type", "system-offset");
227     fgSetLong("/sim/time/cur-time-override", 0);
228
229                                 // Freeze options
230     fgSetBool("/sim/freeze/master", false);
231     fgSetBool("/sim/freeze/position", false);
232     fgSetBool("/sim/freeze/clock", false);
233     fgSetBool("/sim/freeze/fuel", false);
234
235     fgSetString("/sim/multiplay/callsign", "callsign");
236     fgSetString("/sim/multiplay/rxhost", "0");
237     fgSetString("/sim/multiplay/txhost", "0");
238     fgSetInt("/sim/multiplay/rxport", 0);
239     fgSetInt("/sim/multiplay/txport", 0);
240     
241     fgSetString("/sim/version/flightgear", FLIGHTGEAR_VERSION);
242     fgSetString("/sim/version/simgear", SG_STRINGIZE(SIMGEAR_VERSION));
243     fgSetString("/sim/version/openscenegraph", osgGetVersion());
244     fgSetString("/sim/version/revision", REVISION);
245     fgSetInt("/sim/version/build-number", HUDSON_BUILD_NUMBER);
246     fgSetString("/sim/version/build-id", HUDSON_BUILD_ID);
247 }
248
249 static bool
250 parse_wind (const string &wind, double * min_hdg, double * max_hdg,
251             double * speed, double * gust)
252 {
253   string::size_type pos = wind.find('@');
254   if (pos == string::npos)
255     return false;
256   string dir = wind.substr(0, pos);
257   string spd = wind.substr(pos+1);
258   pos = dir.find(':');
259   if (pos == string::npos) {
260     *min_hdg = *max_hdg = atof(dir.c_str());
261   } else {
262     *min_hdg = atof(dir.substr(0,pos).c_str());
263     *max_hdg = atof(dir.substr(pos+1).c_str());
264   }
265   pos = spd.find(':');
266   if (pos == string::npos) {
267     *speed = *gust = atof(spd.c_str());
268   } else {
269     *speed = atof(spd.substr(0,pos).c_str());
270     *gust = atof(spd.substr(pos+1).c_str());
271   }
272   return true;
273 }
274
275 // parse a time string ([+/-]%f[:%f[:%f]]) into hours
276 static double
277 parse_time(const string& time_in) {
278     char *time_str, num[256];
279     double hours, minutes, seconds;
280     double result = 0.0;
281     int sign = 1;
282     int i;
283
284     time_str = (char *)time_in.c_str();
285
286     // printf("parse_time(): %s\n", time_str);
287
288     // check for sign
289     if ( strlen(time_str) ) {
290         if ( time_str[0] == '+' ) {
291             sign = 1;
292             time_str++;
293         } else if ( time_str[0] == '-' ) {
294             sign = -1;
295             time_str++;
296         }
297     }
298     // printf("sign = %d\n", sign);
299
300     // get hours
301     if ( strlen(time_str) ) {
302         i = 0;
303         while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
304             num[i] = time_str[0];
305             time_str++;
306             i++;
307         }
308         if ( time_str[0] == ':' ) {
309             time_str++;
310         }
311         num[i] = '\0';
312         hours = atof(num);
313         // printf("hours = %.2lf\n", hours);
314
315         result += hours;
316     }
317
318     // get minutes
319     if ( strlen(time_str) ) {
320         i = 0;
321         while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
322             num[i] = time_str[0];
323             time_str++;
324             i++;
325         }
326         if ( time_str[0] == ':' ) {
327             time_str++;
328         }
329         num[i] = '\0';
330         minutes = atof(num);
331         // printf("minutes = %.2lf\n", minutes);
332
333         result += minutes / 60.0;
334     }
335
336     // get seconds
337     if ( strlen(time_str) ) {
338         i = 0;
339         while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
340             num[i] = time_str[0];
341             time_str++;
342             i++;
343         }
344         num[i] = '\0';
345         seconds = atof(num);
346         // printf("seconds = %.2lf\n", seconds);
347
348         result += seconds / 3600.0;
349     }
350
351     SG_LOG( SG_GENERAL, SG_INFO, " parse_time() = " << sign * result );
352
353     return(sign * result);
354 }
355
356
357 // parse a date string (yyyy:mm:dd:hh:mm:ss) into a time_t (seconds)
358 static long int 
359 parse_date( const string& date)
360 {
361     struct tm gmt;
362     char * date_str, num[256];
363     int i;
364     // initialize to zero
365     gmt.tm_sec = 0;
366     gmt.tm_min = 0;
367     gmt.tm_hour = 0;
368     gmt.tm_mday = 0;
369     gmt.tm_mon = 0;
370     gmt.tm_year = 0;
371     gmt.tm_isdst = 0; // ignore daylight savings time for the moment
372     date_str = (char *)date.c_str();
373     // get year
374     if ( strlen(date_str) ) {
375         i = 0;
376         while ( (date_str[0] != ':') && (date_str[0] != '\0') ) {
377             num[i] = date_str[0];
378             date_str++;
379             i++;
380         }
381         if ( date_str[0] == ':' ) {
382             date_str++;
383         }
384         num[i] = '\0';
385         gmt.tm_year = atoi(num) - 1900;
386     }
387     // get month
388     if ( strlen(date_str) ) {
389         i = 0;
390         while ( (date_str[0] != ':') && (date_str[0] != '\0') ) {
391             num[i] = date_str[0];
392             date_str++;
393             i++;
394         }
395         if ( date_str[0] == ':' ) {
396             date_str++;
397         }
398         num[i] = '\0';
399         gmt.tm_mon = atoi(num) -1;
400     }
401     // get day
402     if ( strlen(date_str) ) {
403         i = 0;
404         while ( (date_str[0] != ':') && (date_str[0] != '\0') ) {
405             num[i] = date_str[0];
406             date_str++;
407             i++;
408         }
409         if ( date_str[0] == ':' ) {
410             date_str++;
411         }
412         num[i] = '\0';
413         gmt.tm_mday = atoi(num);
414     }
415     // get hour
416     if ( strlen(date_str) ) {
417         i = 0;
418         while ( (date_str[0] != ':') && (date_str[0] != '\0') ) {
419             num[i] = date_str[0];
420             date_str++;
421             i++;
422         }
423         if ( date_str[0] == ':' ) {
424             date_str++;
425         }
426         num[i] = '\0';
427         gmt.tm_hour = atoi(num);
428     }
429     // get minute
430     if ( strlen(date_str) ) {
431         i = 0;
432         while ( (date_str[0] != ':') && (date_str[0] != '\0') ) {
433             num[i] = date_str[0];
434             date_str++;
435             i++;
436         }
437         if ( date_str[0] == ':' ) {
438             date_str++;
439         }
440         num[i] = '\0';
441         gmt.tm_min = atoi(num);
442     }
443     // get second
444     if ( strlen(date_str) ) {
445         i = 0;
446         while ( (date_str[0] != ':') && (date_str[0] != '\0') ) {
447             num[i] = date_str[0];
448             date_str++;
449             i++;
450         }
451         if ( date_str[0] == ':' ) {
452             date_str++;
453         }
454         num[i] = '\0';
455         gmt.tm_sec = atoi(num);
456     }
457     time_t theTime = sgTimeGetGMT( gmt.tm_year, gmt.tm_mon, gmt.tm_mday,
458                                    gmt.tm_hour, gmt.tm_min, gmt.tm_sec );
459     //printf ("Date is %s\n", ctime(&theTime));
460     //printf ("in seconds that is %d\n", theTime);
461     //exit(1);
462     return (theTime);
463 }
464
465
466 // parse angle in the form of [+/-]ddd:mm:ss into degrees
467 static double
468 parse_degree( const string& degree_str) {
469     double result = parse_time( degree_str );
470
471     // printf("Degree = %.4f\n", result);
472
473     return(result);
474 }
475
476
477 // parse time offset string into seconds
478 static int
479 parse_time_offset( const string& time_str) {
480     int result;
481
482     // printf("time offset = %s\n", time_str);
483
484 #ifdef HAVE_RINT
485     result = (int)rint(parse_time(time_str) * 3600.0);
486 #else
487     result = (int)(parse_time(time_str) * 3600.0);
488 #endif
489
490     // printf("parse_time_offset(): %d\n", result);
491
492     return( result );
493 }
494
495
496 // Parse --fov=x.xx type option 
497 static double
498 parse_fov( const string& arg ) {
499     double fov = atof(arg);
500
501     if ( fov < FG_FOV_MIN ) { fov = FG_FOV_MIN; }
502     if ( fov > FG_FOV_MAX ) { fov = FG_FOV_MAX; }
503
504     fgSetDouble("/sim/view[0]/config/default-field-of-view-deg", fov);
505
506     // printf("parse_fov(): result = %.4f\n", fov);
507
508     return fov;
509 }
510
511
512 // Parse I/O channel option
513 //
514 // Format is "--protocol=medium,direction,hz,medium_options,..."
515 //
516 //   protocol = { native, nmea, garmin, AV400, AV400Sim, fgfs, rul, pve, etc. }
517 //   medium = { serial, socket, file, etc. }
518 //   direction = { in, out, bi }
519 //   hz = number of times to process channel per second (floating
520 //        point values are ok.
521 //
522 // Serial example "--nmea=serial,dir,hz,device,baud" where
523 // 
524 //  device = OS device name of serial line to be open()'ed
525 //  baud = {300, 1200, 2400, ..., 230400}
526 //
527 // Socket exacmple "--native=socket,dir,hz,machine,port,style" where
528 //
529 //  machine = machine name or ip address if client (leave empty if server)
530 //  port = port, leave empty to let system choose
531 //  style = tcp or udp
532 //
533 // File example "--garmin=file,dir,hz,filename" where
534 //
535 //  filename = file system file name
536
537 static bool
538 add_channel( const string& type, const string& channel_str ) {
539     // This check is neccessary to prevent fgviewer from segfaulting when given
540     // weird options. (It doesn't run the full initailization)
541     if(!globals->get_channel_options_list())
542     {
543         SG_LOG(SG_GENERAL, SG_ALERT, "Option " << type << "=" << channel_str
544                                      << " ignored.");
545         return false;
546     }
547     SG_LOG(SG_GENERAL, SG_INFO, "Channel string = " << channel_str );
548     globals->get_channel_options_list()->push_back( type + "," + channel_str );
549     return true;
550 }
551
552
553 // The parse wp and parse flight-plan options don't work anymore, because
554 // the route manager and the airport subsystems have not yet been initialized
555 // at this stage.
556
557 // Parse --wp=ID[@alt]
558 static void
559 parse_wp( const string& arg ) {
560     string_list *waypoints = globals->get_initial_waypoints();
561     if (!waypoints) {
562         waypoints = new string_list;
563         globals->set_initial_waypoints(waypoints);
564     }
565     waypoints->push_back(arg);
566 }
567
568
569 // Parse --flight-plan=[file]
570 static bool
571 parse_flightplan(const string& arg)
572 {
573     string_list *waypoints = globals->get_initial_waypoints();
574     if (!waypoints) {
575         waypoints = new string_list;
576         globals->set_initial_waypoints(waypoints);
577     }
578
579     sg_gzifstream in(arg.c_str());
580     if ( !in.is_open() )
581         return false;
582
583     while ( true ) {
584         string line;
585         getline( in, line, '\n' );
586
587         // catch extraneous (DOS) line ending character
588         if ( line[line.length() - 1] < 32 )
589             line = line.substr( 0, line.length()-1 );
590
591         if ( in.eof() )
592             break;
593
594         waypoints->push_back(line);
595     }
596     return true;
597 }
598
599
600 static int
601 fgOptLanguage( const char *arg )
602 {
603     globals->set_locale( fgInitLocale( arg ) );
604     return FG_OPTIONS_OK;
605 }
606
607 static void
608 clearLocation ()
609 {
610     fgSetString("/sim/presets/airport-id", "");
611     fgSetString("/sim/presets/vor-id", "");
612     fgSetString("/sim/presets/ndb-id", "");
613     fgSetString("/sim/presets/carrier", "");
614     fgSetString("/sim/presets/parkpos", "");
615     fgSetString("/sim/presets/fix", "");
616 }
617
618 static int
619 fgOptVOR( const char * arg )
620 {
621     clearLocation();
622     fgSetString("/sim/presets/vor-id", arg);
623     return FG_OPTIONS_OK;
624 }
625
626 static int
627 fgOptNDB( const char * arg )
628 {
629     clearLocation();
630     fgSetString("/sim/presets/ndb-id", arg);
631     return FG_OPTIONS_OK;
632 }
633
634 static int
635 fgOptCarrier( const char * arg )
636 {
637     clearLocation();
638     fgSetString("/sim/presets/carrier", arg);
639     return FG_OPTIONS_OK;
640 }
641
642 static int
643 fgOptParkpos( const char * arg )
644 {
645     fgSetString("/sim/presets/parkpos", arg);
646     return FG_OPTIONS_OK;
647 }
648
649 static int
650 fgOptFIX( const char * arg )
651 {
652     clearLocation();
653     fgSetString("/sim/presets/fix", arg);
654     return FG_OPTIONS_OK;
655 }
656
657 static int
658 fgOptLon( const char *arg )
659 {
660     clearLocation();
661     fgSetDouble("/sim/presets/longitude-deg", parse_degree( arg ));
662     fgSetDouble("/position/longitude-deg", parse_degree( arg ));
663     return FG_OPTIONS_OK;
664 }
665
666 static int
667 fgOptLat( const char *arg )
668 {
669     clearLocation();
670     fgSetDouble("/sim/presets/latitude-deg", parse_degree( arg ));
671     fgSetDouble("/position/latitude-deg", parse_degree( arg ));
672     return FG_OPTIONS_OK;
673 }
674
675 static int
676 fgOptAltitude( const char *arg )
677 {
678     fgSetBool("/sim/presets/onground", false);
679     if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
680         fgSetDouble("/sim/presets/altitude-ft", atof( arg ));
681     else
682         fgSetDouble("/sim/presets/altitude-ft",
683                     atof( arg ) * SG_METER_TO_FEET);
684     return FG_OPTIONS_OK;
685 }
686
687 static int
688 fgOptUBody( const char *arg )
689 {
690     fgSetString("/sim/presets/speed-set", "UVW");
691     if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
692         fgSetDouble("/sim/presets/uBody-fps", atof( arg ));
693     else
694         fgSetDouble("/sim/presets/uBody-fps",
695                     atof( arg ) * SG_METER_TO_FEET);
696     return FG_OPTIONS_OK;
697 }
698
699 static int
700 fgOptVBody( const char *arg )
701 {
702     fgSetString("/sim/presets/speed-set", "UVW");
703     if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
704         fgSetDouble("/sim/presets/vBody-fps", atof( arg ));
705     else
706         fgSetDouble("/sim/presets/vBody-fps",
707                             atof( arg ) * SG_METER_TO_FEET);
708     return FG_OPTIONS_OK;
709 }
710
711 static int
712 fgOptWBody( const char *arg )
713 {
714     fgSetString("/sim/presets/speed-set", "UVW");
715     if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
716         fgSetDouble("/sim/presets/wBody-fps", atof(arg));
717     else
718         fgSetDouble("/sim/presets/wBody-fps",
719                             atof(arg) * SG_METER_TO_FEET);
720     return FG_OPTIONS_OK;
721 }
722
723 static int
724 fgOptVNorth( const char *arg )
725 {
726     fgSetString("/sim/presets/speed-set", "NED");
727     if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
728         fgSetDouble("/sim/presets/speed-north-fps", atof( arg ));
729     else
730         fgSetDouble("/sim/presets/speed-north-fps",
731                             atof( arg ) * SG_METER_TO_FEET);
732     return FG_OPTIONS_OK;
733 }
734
735 static int
736 fgOptVEast( const char *arg )
737 {
738     fgSetString("/sim/presets/speed-set", "NED");
739     if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
740         fgSetDouble("/sim/presets/speed-east-fps", atof(arg));
741     else
742         fgSetDouble("/sim/presets/speed-east-fps",
743                     atof(arg) * SG_METER_TO_FEET);
744     return FG_OPTIONS_OK;
745 }
746
747 static int
748 fgOptVDown( const char *arg )
749 {
750     fgSetString("/sim/presets/speed-set", "NED");
751     if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
752         fgSetDouble("/sim/presets/speed-down-fps", atof(arg));
753     else
754         fgSetDouble("/sim/presets/speed-down-fps",
755                             atof(arg) * SG_METER_TO_FEET);
756     return FG_OPTIONS_OK;
757 }
758
759 static int
760 fgOptVc( const char *arg )
761 {
762     // fgSetString("/sim/presets/speed-set", "knots");
763     // fgSetDouble("/velocities/airspeed-kt", atof(arg.substr(5)));
764     fgSetString("/sim/presets/speed-set", "knots");
765     fgSetDouble("/sim/presets/airspeed-kt", atof(arg));
766     return FG_OPTIONS_OK;
767 }
768
769 static int
770 fgOptMach( const char *arg )
771 {
772     fgSetString("/sim/presets/speed-set", "mach");
773     fgSetDouble("/sim/presets/mach", atof(arg));
774     return FG_OPTIONS_OK;
775 }
776
777 static int
778 fgOptRoc( const char *arg )
779 {
780     fgSetDouble("/velocities/vertical-speed-fps", atof(arg)/60);
781     return FG_OPTIONS_OK;
782 }
783
784 static int
785 fgOptFgRoot( const char *arg )
786 {
787     // this option is dealt with by fgInitFGRoot
788     return FG_OPTIONS_OK;
789 }
790
791 static int
792 fgOptFgScenery( const char *arg )
793 {
794     globals->set_fg_scenery(arg);
795     return FG_OPTIONS_OK;
796 }
797
798 static int
799 fgOptFgAircraft(const char* arg)
800 {
801   // this option is dealt with by fgInitFGAircraft
802   return FG_OPTIONS_OK;
803 }
804
805 static int
806 fgOptFov( const char *arg )
807 {
808     parse_fov( arg );
809     return FG_OPTIONS_OK;
810 }
811
812 static int
813 fgOptGeometry( const char *arg )
814 {
815     bool geometry_ok = true;
816     int xsize = 0, ysize = 0;
817     string geometry = arg;
818     string::size_type i = geometry.find('x');
819
820     if (i != string::npos) {
821         xsize = atoi(geometry.substr(0, i));
822         ysize = atoi(geometry.substr(i+1));
823     } else {
824         geometry_ok = false;
825     }
826
827     if ( xsize <= 0 || ysize <= 0 ) {
828         xsize = 640;
829         ysize = 480;
830         geometry_ok = false;
831     }
832
833     if ( !geometry_ok ) {
834         SG_LOG( SG_GENERAL, SG_ALERT, "Unknown geometry: " << geometry );
835         SG_LOG( SG_GENERAL, SG_ALERT,
836                 "Setting geometry to " << xsize << 'x' << ysize << '\n');
837     } else {
838         SG_LOG( SG_GENERAL, SG_INFO,
839                 "Setting geometry to " << xsize << 'x' << ysize << '\n');
840         fgSetInt("/sim/startup/xsize", xsize);
841         fgSetInt("/sim/startup/ysize", ysize);
842     }
843     return FG_OPTIONS_OK;
844 }
845
846 static int
847 fgOptBpp( const char *arg )
848 {
849     string bits_per_pix = arg;
850     if ( bits_per_pix == "16" ) {
851         fgSetInt("/sim/rendering/bits-per-pixel", 16);
852     } else if ( bits_per_pix == "24" ) {
853         fgSetInt("/sim/rendering/bits-per-pixel", 24);
854     } else if ( bits_per_pix == "32" ) {
855         fgSetInt("/sim/rendering/bits-per-pixel", 32);
856     } else {
857         SG_LOG(SG_GENERAL, SG_ALERT, "Unsupported bpp " << bits_per_pix);
858     }
859     return FG_OPTIONS_OK;
860 }
861
862 static int
863 fgOptTimeOffset( const char *arg )
864 {
865     fgSetInt("/sim/startup/time-offset",
866                 parse_time_offset( arg ));
867     fgSetString("/sim/startup/time-offset-type", "system-offset");
868     return FG_OPTIONS_OK;
869 }
870
871 static int
872 fgOptStartDateSys( const char *arg )
873 {
874     fgSetInt("/sim/startup/time-offset", parse_date( arg ) );
875     fgSetString("/sim/startup/time-offset-type", "system");
876     return FG_OPTIONS_OK;
877 }
878
879 static int
880 fgOptStartDateLat( const char *arg )
881 {
882     fgSetInt("/sim/startup/time-offset", parse_date( arg ) );
883     fgSetString("/sim/startup/time-offset-type", "latitude");
884     return FG_OPTIONS_OK;
885 }
886
887 static int
888 fgOptStartDateGmt( const char *arg )
889 {
890     fgSetInt("/sim/startup/time-offset", parse_date( arg ) );
891     fgSetString("/sim/startup/time-offset-type", "gmt");
892     return FG_OPTIONS_OK;
893 }
894
895 static int
896 fgSetupProxy( const char *arg )
897 {
898     string options = arg;
899     string host, port, auth;
900     string::size_type pos;
901
902     host = port = auth = "";
903     if ((pos = options.find("@")) != string::npos) 
904         auth = options.substr(0, pos++);
905     else
906         pos = 0;
907
908     host = options.substr(pos, options.size());
909     if ((pos = host.find(":")) != string::npos) {
910         port = host.substr(++pos, host.size());
911         host.erase(--pos, host.size());
912     }
913
914     fgSetString("/sim/presets/proxy/host", host.c_str());
915     fgSetString("/sim/presets/proxy/port", port.c_str());
916     fgSetString("/sim/presets/proxy/authentication", auth.c_str());
917
918     return FG_OPTIONS_OK;
919 }
920
921 static int
922 fgOptTraceRead( const char *arg )
923 {
924     string name = arg;
925     SG_LOG(SG_GENERAL, SG_INFO, "Tracing reads for property " << name);
926     fgGetNode(name.c_str(), true)
927         ->setAttribute(SGPropertyNode::TRACE_READ, true);
928     return FG_OPTIONS_OK;
929 }
930
931 static int
932 fgOptLogLevel( const char *arg )
933 {
934     fgSetString("/sim/logging/classes", "all");
935     fgSetString("/sim/logging/priority", arg);
936
937     string priority = arg;
938     logbuf::set_log_classes(SG_ALL);
939     if (priority == "bulk") {
940       logbuf::set_log_priority(SG_BULK);
941     } else if (priority == "debug") {
942       logbuf::set_log_priority(SG_DEBUG);
943     } else if (priority == "info") {
944       logbuf::set_log_priority(SG_INFO);
945     } else if (priority == "warn") {
946       logbuf::set_log_priority(SG_WARN);
947     } else if (priority == "alert") {
948       logbuf::set_log_priority(SG_ALERT);
949     } else {
950       SG_LOG(SG_GENERAL, SG_WARN, "Unknown logging priority " << priority);
951     }
952     SG_LOG(SG_GENERAL, SG_DEBUG, "Logging priority is " << priority);
953
954     return FG_OPTIONS_OK;
955 }
956
957
958 static int
959 fgOptTraceWrite( const char *arg )
960 {
961     string name = arg;
962     SG_LOG(SG_GENERAL, SG_INFO, "Tracing writes for property " << name);
963     fgGetNode(name.c_str(), true)
964         ->setAttribute(SGPropertyNode::TRACE_WRITE, true);
965     return FG_OPTIONS_OK;
966 }
967
968 static int
969 fgOptViewOffset( const char *arg )
970 {
971     // $$$ begin - added VS Renganathan, 14 Oct 2K
972     // for multi-window outside window imagery
973     string woffset = arg;
974     double default_view_offset = 0.0;
975     if ( woffset == "LEFT" ) {
976             default_view_offset = SGD_PI * 0.25;
977     } else if ( woffset == "RIGHT" ) {
978         default_view_offset = SGD_PI * 1.75;
979     } else if ( woffset == "CENTER" ) {
980         default_view_offset = 0.00;
981     } else {
982         default_view_offset = atof( woffset.c_str() ) * SGD_DEGREES_TO_RADIANS;
983     }
984     /* apparently not used (CLO, 11 Jun 2002) 
985         FGViewer *pilot_view =
986             (FGViewer *)globals->get_viewmgr()->get_view( 0 ); */
987     // this will work without calls to the viewer...
988     fgSetDouble( "/sim/current-view/heading-offset-deg",
989                     default_view_offset  * SGD_RADIANS_TO_DEGREES );
990     // $$$ end - added VS Renganathan, 14 Oct 2K
991     return FG_OPTIONS_OK;
992 }
993
994 static int
995 fgOptVisibilityMeters( const char *arg )
996 {
997     double visibility = atof( arg );
998     fgDefaultWeatherValue("visibility-m", visibility);
999     fgSetDouble("/environment/visibility-m", visibility);
1000     return FG_OPTIONS_OK;
1001 }
1002
1003 static int
1004 fgOptVisibilityMiles( const char *arg )
1005 {
1006     double visibility = atof( arg ) * 5280.0 * SG_FEET_TO_METER;
1007     fgDefaultWeatherValue("visibility-m", visibility);
1008     fgSetDouble("/environment/visibility-m", visibility);
1009     return FG_OPTIONS_OK;
1010 }
1011
1012 static int
1013 fgOptRandomWind( const char *arg )
1014 {
1015     double min_hdg = sg_random() * 360.0;
1016     double max_hdg = min_hdg + (20 - sqrt(sg_random() * 400));
1017     double speed = sg_random() * sg_random() * 40;
1018     double gust = speed + (10 - sqrt(sg_random() * 100));
1019     fgSetupWind(min_hdg, max_hdg, speed, gust);
1020     return FG_OPTIONS_OK;
1021 }
1022
1023 static int
1024 fgOptWind( const char *arg )
1025 {
1026     double min_hdg = 0.0, max_hdg = 0.0, speed = 0.0, gust = 0.0;
1027     if (!parse_wind( arg, &min_hdg, &max_hdg, &speed, &gust)) {
1028         SG_LOG( SG_GENERAL, SG_ALERT, "bad wind value " << arg );
1029         return FG_OPTIONS_ERROR;
1030     }
1031     fgSetupWind(min_hdg, max_hdg, speed, gust);
1032     return FG_OPTIONS_OK;
1033 }
1034
1035 static int
1036 fgOptTurbulence( const char *arg )
1037 {
1038     fgDefaultWeatherValue("turbulence/magnitude-norm", atof(arg));
1039     return FG_OPTIONS_OK;
1040 }
1041
1042 static int
1043 fgOptCeiling( const char *arg )
1044 {
1045     double elevation, thickness;
1046     string spec = arg;
1047     string::size_type pos = spec.find(':');
1048     if (pos == string::npos) {
1049         elevation = atof(spec.c_str());
1050         thickness = 2000;
1051     } else {
1052         elevation = atof(spec.substr(0, pos).c_str());
1053         thickness = atof(spec.substr(pos + 1).c_str());
1054     }
1055     fgSetDouble("/environment/clouds/layer[0]/elevation-ft", elevation);
1056     fgSetDouble("/environment/clouds/layer[0]/thickness-ft", thickness);
1057     fgSetString("/environment/clouds/layer[0]/coverage", "overcast");
1058     return FG_OPTIONS_OK;
1059 }
1060
1061 static int
1062 fgOptWp( const char *arg )
1063 {
1064     parse_wp( arg );
1065     return FG_OPTIONS_OK;
1066 }
1067
1068 static int
1069 fgOptFlightPlan( const char *arg )
1070 {
1071     parse_flightplan ( arg );
1072     return FG_OPTIONS_OK;
1073 }
1074
1075 static int
1076 fgOptConfig( const char *arg )
1077 {
1078     string file = arg;
1079     try {
1080         readProperties(file, globals->get_props());
1081     } catch (const sg_exception &e) {
1082         string message = "Error loading config file: ";
1083         message += e.getFormattedMessage() + e.getOrigin();
1084         SG_LOG(SG_INPUT, SG_ALERT, message);
1085         exit(2);
1086     }
1087     return FG_OPTIONS_OK;
1088 }
1089
1090 static bool
1091 parse_colon (const string &s, double * val1, double * val2)
1092 {
1093     string::size_type pos = s.find(':');
1094     if (pos == string::npos) {
1095         *val2 = atof(s);
1096         return false;
1097     } else {
1098         *val1 = atof(s.substr(0, pos).c_str());
1099         *val2 = atof(s.substr(pos+1).c_str());
1100         return true;
1101     }
1102 }
1103
1104
1105 static int
1106 fgOptFailure( const char * arg )
1107 {
1108     string a = arg;
1109     if (a == "pitot") {
1110         fgSetBool("/systems/pitot/serviceable", false);
1111     } else if (a == "static") {
1112         fgSetBool("/systems/static/serviceable", false);
1113     } else if (a == "vacuum") {
1114         fgSetBool("/systems/vacuum/serviceable", false);
1115     } else if (a == "electrical") {
1116         fgSetBool("/systems/electrical/serviceable", false);
1117     } else {
1118         SG_LOG(SG_INPUT, SG_ALERT, "Unknown failure mode: " << a);
1119         return FG_OPTIONS_ERROR;
1120     }
1121
1122     return FG_OPTIONS_OK;
1123 }
1124
1125
1126 static int
1127 fgOptNAV1( const char * arg )
1128 {
1129     double radial, freq;
1130     if (parse_colon(arg, &radial, &freq))
1131         fgSetDouble("/instrumentation/nav[0]/radials/selected-deg", radial);
1132     fgSetDouble("/instrumentation/nav[0]/frequencies/selected-mhz", freq);
1133     return FG_OPTIONS_OK;
1134 }
1135
1136 static int
1137 fgOptNAV2( const char * arg )
1138 {
1139     double radial, freq;
1140     if (parse_colon(arg, &radial, &freq))
1141         fgSetDouble("/instrumentation/nav[1]/radials/selected-deg", radial);
1142     fgSetDouble("/instrumentation/nav[1]/frequencies/selected-mhz", freq);
1143     return FG_OPTIONS_OK;
1144 }
1145
1146 static int
1147 fgOptADF( const char * arg )
1148 {
1149     double rot, freq;
1150     if (parse_colon(arg, &rot, &freq))
1151         fgSetDouble("/instrumentation/adf/rotation-deg", rot);
1152     fgSetDouble("/instrumentation/adf/frequencies/selected-khz", freq);
1153     return FG_OPTIONS_OK;
1154 }
1155
1156 static int
1157 fgOptDME( const char *arg )
1158 {
1159     string opt = arg;
1160     if (opt == "nav1") {
1161         fgSetInt("/instrumentation/dme/switch-position", 1);
1162         fgSetString("/instrumentation/dme/frequencies/source",
1163                     "/instrumentation/nav[0]/frequencies/selected-mhz");
1164     } else if (opt == "nav2") {
1165         fgSetInt("/instrumentation/dme/switch-position", 3);
1166         fgSetString("/instrumentation/dme/frequencies/source",
1167                     "/instrumentation/nav[1]/frequencies/selected-mhz");
1168     } else {
1169         fgSetInt("/instrumentation/dme/switch-position", 2);
1170         fgSetString("/instrumentation/dme/frequencies/source",
1171                     "/instrumentation/dme/frequencies/selected-mhz");
1172         fgSetString("/instrumentation/dme/frequencies/selected-mhz", arg);
1173     }
1174     return FG_OPTIONS_OK;
1175 }
1176
1177 static int
1178 fgOptLivery( const char *arg )
1179 {
1180     string opt = arg;
1181     string livery_path = "livery/" + opt;
1182     fgSetString("/sim/model/texture-path", livery_path.c_str() );
1183     return FG_OPTIONS_OK;
1184 }
1185
1186 static int
1187 fgOptScenario( const char *arg )
1188 {
1189     SGPropertyNode_ptr ai_node = fgGetNode( "/sim/ai", true );
1190     vector<SGPropertyNode_ptr> scenarii = ai_node->getChildren( "scenario" );
1191     int index = -1;
1192     for ( size_t i = 0; i < scenarii.size(); ++i ) {
1193         int ind = scenarii[i]->getIndex();
1194         if ( index < ind ) {
1195             index = ind;
1196         }
1197     }
1198     SGPropertyNode_ptr scenario = ai_node->getNode( "scenario", index + 1, true );
1199     scenario->setStringValue( arg );
1200     ai_node->setBoolValue( "enabled", true );
1201     return FG_OPTIONS_OK;
1202 }
1203
1204 static int
1205 fgOptRunway( const char *arg )
1206 {
1207     fgSetString("/sim/presets/runway", arg );
1208     fgSetBool("/sim/presets/runway-requested", true );
1209     return FG_OPTIONS_OK;
1210 }
1211
1212 static int
1213 fgOptParking( const char *arg )
1214 {
1215     cerr << "Processing argument " << arg << endl;
1216     fgSetString("/sim/presets/parking", arg );
1217     fgSetBool  ("/sim/presets/parking-requested", true );
1218     return FG_OPTIONS_OK;
1219 }
1220
1221 static int
1222 fgOptVersion( const char *arg )
1223 {
1224     cerr << "FlightGear version: " << FLIGHTGEAR_VERSION << endl;
1225     cerr << "Revision: " << REVISION << endl;
1226     cerr << "Build-Id: " << HUDSON_BUILD_ID << endl;
1227     cerr << "FG_ROOT=" << globals->get_fg_root() << endl;
1228     cerr << "FG_HOME=" << fgGetString("/sim/fg-home") << endl;
1229     cerr << "FG_SCENERY=";
1230
1231     int didsome = 0;
1232     string_list scn = globals->get_fg_scenery();
1233     for (string_list::const_iterator it = scn.begin(); it != scn.end(); it++)
1234     {
1235         if (didsome) cerr << ":";
1236         didsome++;
1237         cerr << *it;
1238     }
1239     cerr << endl;
1240     cerr << "SimGear version: " << SG_STRINGIZE(SIMGEAR_VERSION) << endl;
1241     cerr << "PLIB version: " << PLIB_VERSION << endl;
1242     return FG_OPTIONS_EXIT;
1243 }
1244
1245 static int
1246 fgOptFpe(const char* arg)
1247 {
1248     // Actually handled in bootstrap.cxx
1249     return FG_OPTIONS_OK;
1250 }
1251
1252 static int
1253 fgOptFgviewer(const char* arg)
1254 {
1255     // Actually handled in bootstrap.cxx
1256     return FG_OPTIONS_OK;
1257 }
1258
1259
1260
1261 static map<string,size_t> fgOptionMap;
1262
1263 /*
1264    option       has_param type        property         b_param s_param  func
1265
1266 where:
1267  option    : name of the option
1268  has_param : option is --name=value if true or --name if false
1269  type      : OPTION_BOOL    - property is a boolean
1270              OPTION_STRING  - property is a string
1271              OPTION_DOUBLE  - property is a double
1272              OPTION_INT     - property is an integer
1273              OPTION_CHANNEL - name of option is the name of a channel
1274              OPTION_FUNC    - the option trigger a function
1275  b_param   : if type==OPTION_BOOL,
1276              value set to the property (has_param is false for boolean)
1277  s_param   : if type==OPTION_STRING,
1278              value set to the property if has_param is false
1279  func      : function called if type==OPTION_FUNC. if has_param is true,
1280              the value is passed to the function as a string, otherwise,
1281              s_param is passed.
1282
1283     For OPTION_DOUBLE and OPTION_INT, the parameter value is converted into a
1284     double or an integer and set to the property.
1285
1286     For OPTION_CHANNEL, add_channel is called with the parameter value as the
1287     argument.
1288 */
1289
1290 enum OptionType { OPTION_BOOL, OPTION_STRING, OPTION_DOUBLE, OPTION_INT, OPTION_CHANNEL, OPTION_FUNC };
1291 struct OptionDesc {
1292     const char *option;
1293     bool has_param;
1294     enum OptionType type;
1295     const char *property;
1296     bool b_param;
1297     const char *s_param;
1298     int (*func)( const char * );
1299     } fgOptionArray[] = {
1300        
1301     {"language",                     true,  OPTION_FUNC,   "", false, "", fgOptLanguage },
1302     {"disable-game-mode",            false, OPTION_BOOL,   "/sim/startup/game-mode", false, "", 0 },
1303     {"enable-game-mode",             false, OPTION_BOOL,   "/sim/startup/game-mode", true, "", 0 },
1304     {"disable-splash-screen",        false, OPTION_BOOL,   "/sim/startup/splash-screen", false, "", 0 },
1305     {"enable-splash-screen",         false, OPTION_BOOL,   "/sim/startup/splash-screen", true, "", 0 },
1306     {"disable-intro-music",          false, OPTION_BOOL,   "/sim/startup/intro-music", false, "", 0 },
1307     {"enable-intro-music",           false, OPTION_BOOL,   "/sim/startup/intro-music", true, "", 0 },
1308     {"disable-mouse-pointer",        false, OPTION_STRING, "/sim/startup/mouse-pointer", false, "disabled", 0 },
1309     {"enable-mouse-pointer",         false, OPTION_STRING, "/sim/startup/mouse-pointer", false, "enabled", 0 },
1310     {"disable-random-objects",       false, OPTION_BOOL,   "/sim/rendering/random-objects", false, "", 0 },
1311     {"enable-random-objects",        false, OPTION_BOOL,   "/sim/rendering/random-objects", true, "", 0 },
1312     {"disable-real-weather-fetch",   false, OPTION_BOOL,   "/environment/realwx/enabled", false, "", 0 },
1313     {"enable-real-weather-fetch",    false, OPTION_BOOL,   "/environment/realwx/enabled", true,  "", 0 },
1314     {"metar",                        true,  OPTION_STRING, "/environment/metar/data", false, "", 0 },
1315     {"disable-ai-models",            false, OPTION_BOOL,   "/sim/ai/enabled", false, "", 0 },
1316     {"enable-ai-models",             false, OPTION_BOOL,   "/sim/ai/enabled", true, "", 0 },
1317     {"disable-freeze",               false, OPTION_BOOL,   "/sim/freeze/master", false, "", 0 },
1318     {"enable-freeze",                false, OPTION_BOOL,   "/sim/freeze/master", true, "", 0 },
1319     {"disable-fuel-freeze",          false, OPTION_BOOL,   "/sim/freeze/fuel", false, "", 0 },
1320     {"enable-fuel-freeze",           false, OPTION_BOOL,   "/sim/freeze/fuel", true, "", 0 },
1321     {"disable-clock-freeze",         false, OPTION_BOOL,   "/sim/freeze/clock", false, "", 0 },
1322     {"enable-clock-freeze",          false, OPTION_BOOL,   "/sim/freeze/clock", true, "", 0 },
1323     {"disable-hud-3d",               false, OPTION_BOOL,   "/sim/hud/enable3d", false, "", 0 },
1324     {"enable-hud-3d",                false, OPTION_BOOL,   "/sim/hud/enable3d", true, "", 0 },
1325     {"disable-anti-alias-hud",       false, OPTION_BOOL,   "/sim/hud/color/antialiased", false, "", 0 },
1326     {"enable-anti-alias-hud",        false, OPTION_BOOL,   "/sim/hud/color/antialiased", true, "", 0 },
1327     {"control",                      true,  OPTION_STRING, "/sim/control-mode", false, "", 0 },
1328     {"disable-auto-coordination",    false, OPTION_BOOL,   "/sim/auto-coordination", false, "", 0 },
1329     {"enable-auto-coordination",     false, OPTION_BOOL,   "/sim/auto-coordination", true, "", 0 },
1330     {"browser-app",                  true,  OPTION_STRING, "/sim/startup/browser-app", false, "", 0 },
1331     {"disable-hud",                  false, OPTION_BOOL,   "/sim/hud/visibility", false, "", 0 },
1332     {"enable-hud",                   false, OPTION_BOOL,   "/sim/hud/visibility", true, "", 0 },
1333     {"disable-panel",                false, OPTION_BOOL,   "/sim/panel/visibility", false, "", 0 },
1334     {"enable-panel",                 false, OPTION_BOOL,   "/sim/panel/visibility", true, "", 0 },
1335     {"disable-sound",                false, OPTION_BOOL,   "/sim/sound/working", false, "", 0 },
1336     {"enable-sound",                 false, OPTION_BOOL,   "/sim/sound/working", true, "", 0 },
1337     {"sound-device",                 true,  OPTION_STRING, "/sim/sound/device-name", false, "", 0 },
1338     {"airport",                      true,  OPTION_STRING, "/sim/presets/airport-id", false, "", 0 },
1339     {"runway",                       true,  OPTION_FUNC,   "", false, "", fgOptRunway },
1340     {"vor",                          true,  OPTION_FUNC,   "", false, "", fgOptVOR },
1341     {"ndb",                          true,  OPTION_FUNC,   "", false, "", fgOptNDB },
1342     {"carrier",                      true,  OPTION_FUNC,   "", false, "", fgOptCarrier },
1343     {"parkpos",                      true,  OPTION_FUNC,   "", false, "", fgOptParkpos },
1344     {"fix",                          true,  OPTION_FUNC,   "", false, "", fgOptFIX },
1345     {"offset-distance",              true,  OPTION_DOUBLE, "/sim/presets/offset-distance-nm", false, "", 0 },
1346     {"offset-azimuth",               true,  OPTION_DOUBLE, "/sim/presets/offset-azimuth-deg", false, "", 0 },
1347     {"lon",                          true,  OPTION_FUNC,   "", false, "", fgOptLon },
1348     {"lat",                          true,  OPTION_FUNC,   "", false, "", fgOptLat },
1349     {"altitude",                     true,  OPTION_FUNC,   "", false, "", fgOptAltitude },
1350     {"uBody",                        true,  OPTION_FUNC,   "", false, "", fgOptUBody },
1351     {"vBody",                        true,  OPTION_FUNC,   "", false, "", fgOptVBody },
1352     {"wBody",                        true,  OPTION_FUNC,   "", false, "", fgOptWBody },
1353     {"vNorth",                       true,  OPTION_FUNC,   "", false, "", fgOptVNorth },
1354     {"vEast",                        true,  OPTION_FUNC,   "", false, "", fgOptVEast },
1355     {"vDown",                        true,  OPTION_FUNC,   "", false, "", fgOptVDown },
1356     {"vc",                           true,  OPTION_FUNC,   "", false, "", fgOptVc },
1357     {"mach",                         true,  OPTION_FUNC,   "", false, "", fgOptMach },
1358     {"heading",                      true,  OPTION_DOUBLE, "/sim/presets/heading-deg", false, "", 0 },
1359     {"roll",                         true,  OPTION_DOUBLE, "/sim/presets/roll-deg", false, "", 0 },
1360     {"pitch",                        true,  OPTION_DOUBLE, "/sim/presets/pitch-deg", false, "", 0 },
1361     {"glideslope",                   true,  OPTION_DOUBLE, "/sim/presets/glideslope-deg", false, "", 0 },
1362     {"roc",                          true,  OPTION_FUNC,   "", false, "", fgOptRoc },
1363     {"fg-root",                      true,  OPTION_FUNC,   "", false, "", fgOptFgRoot },
1364     {"fg-scenery",                   true,  OPTION_FUNC,   "", false, "", fgOptFgScenery },
1365     {"fg-aircraft",                  true,  OPTION_FUNC,   "", false, "", fgOptFgAircraft },
1366     {"fdm",                          true,  OPTION_STRING, "/sim/flight-model", false, "", 0 },
1367     {"aero",                         true,  OPTION_STRING, "/sim/aero", false, "", 0 },
1368     {"aircraft-dir",                 true,  OPTION_STRING, "/sim/aircraft-dir", false, "", 0 },
1369     {"model-hz",                     true,  OPTION_INT,    "/sim/model-hz", false, "", 0 },
1370     {"speed",                        true,  OPTION_INT,    "/sim/speed-up", false, "", 0 },
1371     {"trim",                         false, OPTION_BOOL,   "/sim/presets/trim", true, "", 0 },
1372     {"notrim",                       false, OPTION_BOOL,   "/sim/presets/trim", false, "", 0 },
1373     {"on-ground",                    false, OPTION_BOOL,   "/sim/presets/onground", true, "", 0 },
1374     {"in-air",                       false, OPTION_BOOL,   "/sim/presets/onground", false, "", 0 },
1375     {"fog-disable",                  false, OPTION_STRING, "/sim/rendering/fog", false, "disabled", 0 },
1376     {"fog-fastest",                  false, OPTION_STRING, "/sim/rendering/fog", false, "fastest", 0 },
1377     {"fog-nicest",                   false, OPTION_STRING, "/sim/rendering/fog", false, "nicest", 0 },
1378     {"disable-horizon-effect",       false, OPTION_BOOL,   "/sim/rendering/horizon-effect", false, "", 0 },
1379     {"enable-horizon-effect",        false, OPTION_BOOL,   "/sim/rendering/horizon-effect", true, "", 0 },
1380     {"disable-enhanced-lighting",    false, OPTION_BOOL,   "/sim/rendering/enhanced-lighting", false, "", 0 },
1381     {"enable-enhanced-lighting",     false, OPTION_BOOL,   "/sim/rendering/enhanced-lighting", true, "", 0 },
1382     {"disable-distance-attenuation", false, OPTION_BOOL,   "/sim/rendering/distance-attenuation", false, "", 0 },
1383     {"enable-distance-attenuation",  false, OPTION_BOOL,   "/sim/rendering/distance-attenuation", true, "", 0 },
1384     {"disable-specular-highlight",   false, OPTION_BOOL,   "/sim/rendering/specular-highlight", false, "", 0 },
1385     {"enable-specular-highlight",    false, OPTION_BOOL,   "/sim/rendering/specular-highlight", true, "", 0 },
1386     {"disable-clouds",               false, OPTION_BOOL,   "/environment/clouds/status", false, "", 0 },
1387     {"enable-clouds",                false, OPTION_BOOL,   "/environment/clouds/status", true, "", 0 },
1388     {"disable-clouds3d",             false, OPTION_BOOL,   "/sim/rendering/clouds3d-enable", false, "", 0 },
1389     {"enable-clouds3d",              false, OPTION_BOOL,   "/sim/rendering/clouds3d-enable", true, "", 0 },
1390     {"fov",                          true,  OPTION_FUNC,   "", false, "", fgOptFov },
1391     {"aspect-ratio-multiplier",      true,  OPTION_DOUBLE, "/sim/current-view/aspect-ratio-multiplier", false, "", 0 },
1392     {"disable-fullscreen",           false, OPTION_BOOL,   "/sim/startup/fullscreen", false, "", 0 },
1393     {"enable-fullscreen",            false, OPTION_BOOL,   "/sim/startup/fullscreen", true, "", 0 },
1394     {"disable-save-on-exit",         false, OPTION_BOOL,   "/sim/startup/save-on-exit", false, "", 0 },
1395     {"enable-save-on-exit",          false, OPTION_BOOL,   "/sim/startup/save-on-exit", true, "", 0 },
1396     {"shading-flat",                 false, OPTION_BOOL,   "/sim/rendering/shading", false, "", 0 },
1397     {"shading-smooth",               false, OPTION_BOOL,   "/sim/rendering/shading", true, "", 0 },
1398     {"disable-skyblend",             false, OPTION_BOOL,   "/sim/rendering/skyblend", false, "", 0 },
1399     {"enable-skyblend",              false, OPTION_BOOL,   "/sim/rendering/skyblend", true, "", 0 },
1400     {"disable-textures",             false, OPTION_BOOL,   "/sim/rendering/textures", false, "", 0 },
1401     {"enable-textures",              false, OPTION_BOOL,   "/sim/rendering/textures", true, "", 0 },
1402     {"texture-filtering",            false, OPTION_INT,    "/sim/rendering/filtering", 1, "", 0 },
1403     {"disable-wireframe",            false, OPTION_BOOL,   "/sim/rendering/wireframe", false, "", 0 },
1404     {"enable-wireframe",             false, OPTION_BOOL,   "/sim/rendering/wireframe", true, "", 0 },
1405     {"geometry",                     true,  OPTION_FUNC,   "", false, "", fgOptGeometry },
1406     {"bpp",                          true,  OPTION_FUNC,   "", false, "", fgOptBpp },
1407     {"units-feet",                   false, OPTION_STRING, "/sim/startup/units", false, "feet", 0 },
1408     {"units-meters",                 false, OPTION_STRING, "/sim/startup/units", false, "meters", 0 },
1409     {"timeofday",                    true,  OPTION_STRING, "/sim/startup/time-offset-type", false, "noon", 0 },
1410     {"season",                       true,  OPTION_STRING, "/sim/startup/season", false, "summer", 0 },
1411     {"time-offset",                  true,  OPTION_FUNC,   "", false, "", fgOptTimeOffset },
1412     {"time-match-real",              false, OPTION_STRING, "/sim/startup/time-offset-type", false, "system-offset", 0 },
1413     {"time-match-local",             false, OPTION_STRING, "/sim/startup/time-offset-type", false, "latitude-offset", 0 },
1414     {"start-date-sys",               true,  OPTION_FUNC,   "", false, "", fgOptStartDateSys },
1415     {"start-date-lat",               true,  OPTION_FUNC,   "", false, "", fgOptStartDateLat },
1416     {"start-date-gmt",               true,  OPTION_FUNC,   "", false, "", fgOptStartDateGmt },
1417     {"hud-tris",                     false, OPTION_STRING, "/sim/hud/frame-stat-type", false, "tris", 0 },
1418     {"hud-culled",                   false, OPTION_STRING, "/sim/hud/frame-stat-type", false, "culled", 0 },
1419     {"atcsim",                       true,  OPTION_CHANNEL, "", false, "dummy", 0 },
1420     {"atlas",                        true,  OPTION_CHANNEL, "", false, "", 0 },
1421     {"httpd",                        true,  OPTION_CHANNEL, "", false, "", 0 },
1422 #ifdef FG_JPEG_SERVER
1423     {"jpg-httpd",                    true,  OPTION_CHANNEL, "", false, "", 0 },
1424 #endif
1425     {"native",                       true,  OPTION_CHANNEL, "", false, "", 0 },
1426     {"native-ctrls",                 true,  OPTION_CHANNEL, "", false, "", 0 },
1427     {"native-fdm",                   true,  OPTION_CHANNEL, "", false, "", 0 },
1428     {"native-gui",                   true,  OPTION_CHANNEL, "", false, "", 0 },
1429     {"opengc",                       true,  OPTION_CHANNEL, "", false, "", 0 },
1430     {"AV400",                        true,  OPTION_CHANNEL, "", false, "", 0 },
1431     {"AV400Sim",                     true,  OPTION_CHANNEL, "", false, "", 0 },
1432     {"garmin",                       true,  OPTION_CHANNEL, "", false, "", 0 },
1433     {"nmea",                         true,  OPTION_CHANNEL, "", false, "", 0 },
1434     {"generic",                      true,  OPTION_CHANNEL, "", false, "", 0 },
1435     {"props",                        true,  OPTION_CHANNEL, "", false, "", 0 },
1436     {"telnet",                       true,  OPTION_CHANNEL, "", false, "", 0 },
1437     {"pve",                          true,  OPTION_CHANNEL, "", false, "", 0 },
1438     {"ray",                          true,  OPTION_CHANNEL, "", false, "", 0 },
1439     {"rul",                          true,  OPTION_CHANNEL, "", false, "", 0 },
1440     {"joyclient",                    true,  OPTION_CHANNEL, "", false, "", 0 },
1441     {"jsclient",                     true,  OPTION_CHANNEL, "", false, "", 0 },
1442     {"proxy",                        true,  OPTION_FUNC,    "", false, "", fgSetupProxy },
1443     {"callsign",                     true, OPTION_STRING,  "sim/multiplay/callsign", false, "", 0 },
1444     {"multiplay",                    true,  OPTION_CHANNEL, "", false, "", 0 },
1445     {"trace-read",                   true,  OPTION_FUNC,   "", false, "", fgOptTraceRead },
1446     {"trace-write",                  true,  OPTION_FUNC,   "", false, "", fgOptTraceWrite },
1447     {"log-level",                    true,  OPTION_FUNC,   "", false, "", fgOptLogLevel },
1448     {"view-offset",                  true,  OPTION_FUNC,   "", false, "", fgOptViewOffset },
1449     {"visibility",                   true,  OPTION_FUNC,   "", false, "", fgOptVisibilityMeters },
1450     {"visibility-miles",             true,  OPTION_FUNC,   "", false, "", fgOptVisibilityMiles },
1451     {"random-wind",                  false, OPTION_FUNC,   "", false, "", fgOptRandomWind },
1452     {"wind",                         true,  OPTION_FUNC,   "", false, "", fgOptWind },
1453     {"turbulence",                   true,  OPTION_FUNC,   "", false, "", fgOptTurbulence },
1454     {"ceiling",                      true,  OPTION_FUNC,   "", false, "", fgOptCeiling },
1455     {"wp",                           true,  OPTION_FUNC,   "", false, "", fgOptWp },
1456     {"flight-plan",                  true,  OPTION_FUNC,   "", false, "", fgOptFlightPlan },
1457     {"config",                       true,  OPTION_FUNC,   "", false, "", fgOptConfig },
1458     {"aircraft",                     true,  OPTION_STRING, "/sim/aircraft", false, "", 0 },
1459     {"vehicle",                      true,  OPTION_STRING, "/sim/aircraft", false, "", 0 },
1460     {"failure",                      true,  OPTION_FUNC,   "", false, "", fgOptFailure },
1461     {"com1",                         true,  OPTION_DOUBLE, "/instrumentation/comm[0]/frequencies/selected-mhz", false, "", 0 },
1462     {"com2",                         true,  OPTION_DOUBLE, "/instrumentation/comm[1]/frequencies/selected-mhz", false, "", 0 },
1463     {"nav1",                         true,  OPTION_FUNC,   "", false, "", fgOptNAV1 },
1464     {"nav2",                         true,  OPTION_FUNC,   "", false, "", fgOptNAV2 },
1465     {"adf",                          true,  OPTION_FUNC,   "", false, "", fgOptADF },
1466     {"dme",                          true,  OPTION_FUNC,   "", false, "", fgOptDME },
1467     {"min-status",                   true,  OPTION_STRING,  "/sim/aircraft-min-status", false, "all", 0 },
1468     {"livery",                       true,  OPTION_FUNC,   "", false, "", fgOptLivery },
1469     {"ai-scenario",                  true,  OPTION_FUNC,   "", false, "", fgOptScenario },
1470     {"parking-id",                   true,  OPTION_FUNC,   "", false, "", fgOptParking  },
1471     {"version",                      false, OPTION_FUNC,   "", false, "", fgOptVersion },
1472     {"enable-fpe",                   false, OPTION_FUNC,   "", false, "", fgOptFpe},
1473     {"fgviewer",                     false, OPTION_FUNC,   "", false, "", fgOptFgviewer},
1474     {0}
1475 };
1476
1477
1478 // Set a property for the --prop: option. Syntax: --prop:[<type>:]<name>=<value>
1479 // <type> can be "double" etc. but also only the first letter "d".
1480 // Examples:  --prop:alpha=1  --prop:bool:beta=true  --prop:d:gamma=0.123
1481 static bool
1482 set_property(const string& arg)
1483 {
1484     string::size_type pos = arg.find('=');
1485     if (pos == arg.npos || pos == 0 || pos + 1 == arg.size())
1486         return false;
1487
1488     string name = arg.substr(0, pos);
1489     string value = arg.substr(pos + 1);
1490     string type;
1491     pos = name.find(':');
1492
1493     if (pos != name.npos && pos != 0 && pos + 1 != name.size()) {
1494         type = name.substr(0, pos);
1495         name = name.substr(pos + 1);
1496     }
1497     SGPropertyNode *n = fgGetNode(name.c_str(), true);
1498
1499     bool writable = n->getAttribute(SGPropertyNode::WRITE);
1500     if (!writable)
1501         n->setAttribute(SGPropertyNode::WRITE, true);
1502
1503     bool ret = false;
1504     if (type.empty())
1505         ret = n->setUnspecifiedValue(value.c_str());
1506     else if (type == "s" || type == "string")
1507         ret = n->setStringValue(value.c_str());
1508     else if (type == "d" || type == "double")
1509         ret = n->setDoubleValue(strtod(value.c_str(), 0));
1510     else if (type == "f" || type == "float")
1511         ret = n->setFloatValue(atof(value.c_str()));
1512     else if (type == "l" || type == "long")
1513         ret =  n->setLongValue(strtol(value.c_str(), 0, 0));
1514     else if (type == "i" || type == "int")
1515         ret =  n->setIntValue(atoi(value.c_str()));
1516     else if (type == "b" || type == "bool")
1517         ret =  n->setBoolValue(value == "true" || atoi(value.c_str()) != 0);
1518
1519     if (!writable)
1520         n->setAttribute(SGPropertyNode::WRITE, false);
1521     return ret;
1522 }
1523
1524
1525 // Parse a single option
1526 static int
1527 parse_option (const string& arg)
1528 {
1529     if ( fgOptionMap.size() == 0 ) {
1530         size_t i = 0;
1531         OptionDesc *pt = &fgOptionArray[ 0 ];
1532         while ( pt->option != 0 ) {
1533             fgOptionMap[ pt->option ] = i;
1534             i += 1;
1535             pt += 1;
1536         }
1537     }
1538
1539     // General Options
1540     if ( (arg == "--help") || (arg == "-h") ) {
1541         // help/usage request
1542         return(FG_OPTIONS_HELP);
1543     } else if ( (arg == "--verbose") || (arg == "-v") ) {
1544         // verbose help/usage request
1545         return(FG_OPTIONS_VERBOSE_HELP);
1546     } else if ( arg.find( "--show-aircraft") == 0) {
1547         return(FG_OPTIONS_SHOW_AIRCRAFT);
1548     } else if ( arg.find( "--show-sound-devices") == 0) {
1549         return(FG_OPTIONS_SHOW_SOUND_DEVICES);
1550     } else if ( arg.find( "--prop:" ) == 0 ) {
1551         if (!set_property(arg.substr(7))) {
1552             SG_LOG( SG_GENERAL, SG_ALERT, "Bad property assignment: " << arg );
1553             return FG_OPTIONS_ERROR;
1554         }
1555     } else if ( arg.find("-psn_") == 0) {
1556     // on Mac, when launched from the GUI, we are passed the ProcessSerialNumber
1557     // as an argument (and no others). Silently ignore the argument here.
1558         return FG_OPTIONS_OK;
1559     } else if ( arg.find( "--" ) == 0 ) {
1560         size_t pos = arg.find( '=' );
1561         string arg_name, arg_value;
1562         if ( pos == string::npos ) {
1563             arg_name = arg.substr( 2 );
1564         } else {
1565             arg_name = arg.substr( 2, pos - 2 );
1566             arg_value = arg.substr( pos + 1);
1567         }
1568         map<string,size_t>::iterator it = fgOptionMap.find( arg_name );
1569         if ( it != fgOptionMap.end() ) {
1570             OptionDesc *pt = &fgOptionArray[ it->second ];
1571             switch ( pt->type ) {
1572                 case OPTION_BOOL:
1573                     fgSetBool( pt->property, pt->b_param );
1574                     break;
1575                 case OPTION_STRING:
1576                     if ( pt->has_param && !arg_value.empty() ) {
1577                         fgSetString( pt->property, arg_value.c_str() );
1578                     } else if ( !pt->has_param && arg_value.empty() ) {
1579                         fgSetString( pt->property, pt->s_param );
1580                     } else if ( pt->has_param ) {
1581                         SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << arg << "' needs a parameter" );
1582                         return FG_OPTIONS_ERROR;
1583                     } else {
1584                         SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << arg << "' does not have a parameter" );
1585                         return FG_OPTIONS_ERROR;
1586                     }
1587                     break;
1588                 case OPTION_DOUBLE:
1589                     if ( !arg_value.empty() ) {
1590                         fgSetDouble( pt->property, atof( arg_value ) );
1591                     } else {
1592                         SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << arg << "' needs a parameter" );
1593                         return FG_OPTIONS_ERROR;
1594                     }
1595                     break;
1596                 case OPTION_INT:
1597                     if ( !arg_value.empty() ) {
1598                         fgSetInt( pt->property, atoi( arg_value ) );
1599                     } else {
1600                         SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << arg << "' needs a parameter" );
1601                         return FG_OPTIONS_ERROR;
1602                     }
1603                     break;
1604                 case OPTION_CHANNEL:
1605                     // XXX return value of add_channel should be checked?
1606                     if ( pt->has_param && !arg_value.empty() ) {
1607                         add_channel( pt->option, arg_value );
1608                     } else if ( !pt->has_param && arg_value.empty() ) {
1609                         add_channel( pt->option, pt->s_param );
1610                     } else if ( pt->has_param ) {
1611                         SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << arg << "' needs a parameter" );
1612                         return FG_OPTIONS_ERROR;
1613                     } else {
1614                         SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << arg << "' does not have a parameter" );
1615                         return FG_OPTIONS_ERROR;
1616                     }
1617                     break;
1618                 case OPTION_FUNC:
1619                     if ( pt->has_param && !arg_value.empty() ) {
1620                         return pt->func( arg_value.c_str() );
1621                     } else if ( !pt->has_param && arg_value.empty() ) {
1622                         return pt->func( pt->s_param );
1623                     } else if ( pt->has_param ) {
1624                         SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << arg << "' needs a parameter" );
1625                         return FG_OPTIONS_ERROR;
1626                     } else {
1627                         SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << arg << "' does not have a parameter" );
1628                         return FG_OPTIONS_ERROR;
1629                     }
1630                     break;
1631             }
1632         } else {
1633             SG_LOG( SG_GENERAL, SG_ALERT, "Unknown option '" << arg << "'" );
1634             return FG_OPTIONS_ERROR;
1635         }
1636     } else {
1637         SG_LOG( SG_GENERAL, SG_ALERT, "Unknown option '" << arg << "'" );
1638         return FG_OPTIONS_ERROR;
1639     }
1640
1641     return FG_OPTIONS_OK;
1642 }
1643
1644
1645 // Parse the command line options
1646 void
1647 fgParseArgs (int argc, char **argv)
1648 {
1649     bool in_options = true;
1650     bool verbose = false;
1651     bool help = false;
1652
1653     SG_LOG(SG_GENERAL, SG_ALERT, "Processing command line arguments");
1654
1655     for (int i = 1; i < argc; i++) {
1656         string arg = argv[i];
1657
1658         if (in_options && (arg.find('-') == 0)) {
1659           if (arg == "--") {
1660             in_options = false;
1661           } else {
1662             int result = parse_option(arg);
1663             if ((result == FG_OPTIONS_HELP) || (result == FG_OPTIONS_ERROR))
1664               help = true;
1665
1666             else if (result == FG_OPTIONS_VERBOSE_HELP)
1667               verbose = true;
1668
1669             else if (result == FG_OPTIONS_SHOW_AIRCRAFT) {
1670               fgOptLogLevel( "alert" );
1671               SGPath path( globals->get_fg_root() );
1672               path.append("Aircraft");
1673               fgShowAircraft(path);
1674               exit(0);
1675
1676             } else if (result == FG_OPTIONS_SHOW_SOUND_DEVICES) {
1677               SGSoundMgr smgr;
1678
1679               smgr.init();
1680               string vendor = smgr.get_vendor();
1681               string renderer = smgr.get_renderer();
1682               cout << renderer << " provided by " << vendor << endl;
1683               cout << endl << "No. Device" << endl;
1684
1685               vector <const char*>devices = smgr.get_available_devices();
1686               for (vector <const char*>::size_type i=0; i<devices.size(); i++) {
1687                 cout << i << ".  \"" << devices[i] << "\"" << endl;
1688               }
1689               devices.clear();
1690               exit(0);
1691             }
1692
1693             else if (result == FG_OPTIONS_EXIT)
1694                exit(0);
1695           }
1696         } else {
1697           in_options = false;
1698           SG_LOG(SG_GENERAL, SG_INFO,
1699                  "Reading command-line property file " << arg);
1700           readProperties(arg, globals->get_props());
1701         }
1702     }
1703
1704     if (help) {
1705        fgOptLogLevel( "alert" );
1706        fgUsage(verbose);
1707        exit(0);
1708     }
1709
1710     SG_LOG(SG_GENERAL, SG_INFO, "Finished command line arguments");
1711 }
1712
1713
1714 // Parse config file options
1715 void
1716 fgParseOptions (const string& path) {
1717     sg_gzifstream in( path );
1718     if ( !in.is_open() ) {
1719         return;
1720     }
1721
1722     SG_LOG( SG_GENERAL, SG_INFO, "Processing config file: " << path );
1723
1724     in >> skipcomment;
1725     while ( ! in.eof() ) {
1726         string line;
1727         getline( in, line, '\n' );
1728
1729         // catch extraneous (DOS) line ending character
1730         int i;
1731         for (i = line.length(); i > 0; i--)
1732             if (line[i - 1] > 32)
1733                 break;
1734         line = line.substr( 0, i );
1735
1736         if ( parse_option( line ) == FG_OPTIONS_ERROR ) {
1737             cerr << endl << "Config file parse error: " << path << " '" 
1738                     << line << "'" << endl;
1739             fgUsage();
1740             exit(-1);
1741         }
1742         in >> skipcomment;
1743     }
1744 }
1745
1746
1747 // Print usage message
1748 void 
1749 fgUsage (bool verbose)
1750 {
1751     SGPropertyNode *locale = globals->get_locale();
1752
1753     SGPropertyNode options_root;
1754
1755     SG_LOG( SG_GENERAL, SG_ALERT, "" ); // To popup the console on Windows
1756     cout << endl;
1757
1758     try {
1759         fgLoadProps("options.xml", &options_root);
1760     } catch (const sg_exception &) {
1761         cout << "Unable to read the help file." << endl;
1762         cout << "Make sure the file options.xml is located in the FlightGear base directory," << endl;
1763         cout << "and the location of the base directory is specified by setting $FG_ROOT or" << endl;
1764         cout << "by adding --fg-root=path as a program argument." << endl;
1765         
1766         exit(-1);
1767     }
1768
1769     SGPropertyNode *options = options_root.getNode("options");
1770     if (!options) {
1771         SG_LOG( SG_GENERAL, SG_ALERT,
1772                 "Error reading options.xml: <options> directive not found." );
1773         exit(-1);
1774     }
1775
1776     SGPropertyNode *usage = locale->getNode(options->getStringValue("usage"));
1777     if (usage) {
1778         cout << "Usage: " << usage->getStringValue() << endl;
1779     }
1780
1781     vector<SGPropertyNode_ptr>section = options->getChildren("section");
1782     for (unsigned int j = 0; j < section.size(); j++) {
1783         string msg = "";
1784
1785         vector<SGPropertyNode_ptr>option = section[j]->getChildren("option");
1786         for (unsigned int k = 0; k < option.size(); k++) {
1787
1788             SGPropertyNode *name = option[k]->getNode("name");
1789             SGPropertyNode *short_name = option[k]->getNode("short");
1790             SGPropertyNode *key = option[k]->getNode("key");
1791             SGPropertyNode *arg = option[k]->getNode("arg");
1792             bool brief = option[k]->getNode("brief") != 0;
1793
1794             if ((brief || verbose) && name) {
1795                 string tmp = name->getStringValue();
1796
1797                 if (key){
1798                     tmp.append(":");
1799                     tmp.append(key->getStringValue());
1800                 }
1801                 if (arg) {
1802                     tmp.append("=");
1803                     tmp.append(arg->getStringValue());
1804                 }
1805                 if (short_name) {
1806                     tmp.append(", -");
1807                     tmp.append(short_name->getStringValue());
1808                 }
1809                                 
1810                 if (tmp.size() <= 25) {
1811                     msg+= "   --";
1812                     msg += tmp;
1813                     msg.append( 27-tmp.size(), ' ');
1814                 } else {
1815                     msg += "\n   --";
1816                     msg += tmp + '\n';
1817                     msg.append(32, ' ');
1818                 }
1819                 // There may be more than one <description> tag assosiated
1820                 // with one option
1821
1822                 vector<SGPropertyNode_ptr> desc;
1823                 desc = option[k]->getChildren("description");
1824                 if (desc.size() > 0) {
1825                    for ( unsigned int l = 0; l < desc.size(); l++) {
1826
1827                       // There may be more than one translation line.
1828
1829                       string t = desc[l]->getStringValue();
1830                       SGPropertyNode *n = locale->getNode("strings");
1831                       vector<SGPropertyNode_ptr>trans_desc =
1832                                n->getChildren(t.substr(8).c_str());
1833
1834                       for ( unsigned int m = 0; m < trans_desc.size(); m++ ) {
1835                          string t_str = trans_desc[m]->getStringValue();
1836
1837                          if ((m > 0) || ((l > 0) && m == 0)) {
1838                             msg.append( 32, ' ');
1839                          }
1840
1841                          // If the string is too large to fit on the screen,
1842                          // then split it up in several pieces.
1843
1844                          while ( t_str.size() > 47 ) {
1845
1846                             unsigned int m = t_str.rfind(' ', 47);
1847                             msg += t_str.substr(0, m) + '\n';
1848                             msg.append( 32, ' ');
1849
1850                             t_str.erase(t_str.begin(), t_str.begin() + m + 1);
1851                         }
1852                         msg += t_str + '\n';
1853                      }
1854                   }
1855                }
1856             }
1857         }
1858
1859         SGPropertyNode *name;
1860         name = locale->getNode(section[j]->getStringValue("name"));
1861
1862         if (!msg.empty() && name) {
1863            cout << endl << name->getStringValue() << ":" << endl;
1864            cout << msg;
1865            msg.erase();
1866         }
1867     }
1868
1869     if ( !verbose ) {
1870         cout << endl;
1871         cout << "For a complete list of options use --help --verbose" << endl;
1872     }
1873 #ifdef _MSC_VER
1874     cout << "Hit a key to continue..." << endl;
1875     cin.get();
1876 #endif
1877 }