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