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