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