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