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