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