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