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