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