]> git.mxchange.org Git - flightgear.git/blob - src/Main/options.cxx
Tweak to the --atc610x option.
[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 static int
632 fgOptLanguage( const char *arg )
633 {
634     globals->set_locale( fgInitLocale( arg ) );
635     return FG_OPTIONS_OK;
636 }
637
638 static void
639 clearLocation ()
640 {
641     fgSetString("/sim/presets/airport-id", "");
642     fgSetString("/sim/presets/vor-id", "");
643     fgSetString("/sim/presets/ndb-id", "");
644     fgSetString("/sim/presets/fix", "");
645 }
646
647 static int
648 fgOptVOR( const char * arg )
649 {
650     clearLocation();
651     fgSetString("/sim/presets/vor-id", arg);
652     return FG_OPTIONS_OK;
653 }
654
655 static int
656 fgOptNDB( const char * arg )
657 {
658     clearLocation();
659     fgSetString("/sim/presets/ndb-id", arg);
660     return FG_OPTIONS_OK;
661 }
662
663 static int
664 fgOptFIX( const char * arg )
665 {
666     clearLocation();
667     fgSetString("/sim/presets/fix", arg);
668     return FG_OPTIONS_OK;
669 }
670
671 static int
672 fgOptLon( const char *arg )
673 {
674     clearLocation();
675     fgSetDouble("/sim/presets/longitude-deg", parse_degree( arg ));
676     fgSetDouble("/position/longitude-deg", parse_degree( arg ));
677     return FG_OPTIONS_OK;
678 }
679
680 static int
681 fgOptLat( const char *arg )
682 {
683     clearLocation();
684     fgSetDouble("/sim/presets/latitude-deg", parse_degree( arg ));
685     fgSetDouble("/position/latitude-deg", parse_degree( arg ));
686     return FG_OPTIONS_OK;
687 }
688
689 static int
690 fgOptAltitude( const char *arg )
691 {
692     fgSetBool("/sim/presets/onground", false);
693     if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
694         fgSetDouble("/sim/presets/altitude-ft", atof( arg ));
695     else
696         fgSetDouble("/sim/presets/altitude-ft",
697                     atof( arg ) * SG_METER_TO_FEET);
698     return FG_OPTIONS_OK;
699 }
700
701 static int
702 fgOptUBody( const char *arg )
703 {
704     fgSetString("/sim/presets/speed-set", "UVW");
705     if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
706         fgSetDouble("/sim/presets/uBody-fps", atof( arg ));
707     else
708         fgSetDouble("/sim/presets/uBody-fps",
709                     atof( arg ) * SG_METER_TO_FEET);
710     return FG_OPTIONS_OK;
711 }
712
713 static int
714 fgOptVBody( const char *arg )
715 {
716     fgSetString("/sim/presets/speed-set", "UVW");
717     if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
718         fgSetDouble("/sim/presets/vBody-fps", atof( arg ));
719     else
720         fgSetDouble("/sim/presets/vBody-fps",
721                             atof( arg ) * SG_METER_TO_FEET);
722     return FG_OPTIONS_OK;
723 }
724
725 static int
726 fgOptWBody( const char *arg )
727 {
728     fgSetString("/sim/presets/speed-set", "UVW");
729     if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
730         fgSetDouble("/sim/presets/wBody-fps", atof(arg));
731     else
732         fgSetDouble("/sim/presets/wBody-fps",
733                             atof(arg) * SG_METER_TO_FEET);
734     return FG_OPTIONS_OK;
735 }
736
737 static int
738 fgOptVNorth( const char *arg )
739 {
740     fgSetString("/sim/presets/speed-set", "NED");
741     if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
742         fgSetDouble("/sim/presets/speed-north-fps", atof( arg ));
743     else
744         fgSetDouble("/sim/presets/speed-north-fps",
745                             atof( arg ) * SG_METER_TO_FEET);
746     return FG_OPTIONS_OK;
747 }
748
749 static int
750 fgOptVEast( const char *arg )
751 {
752     fgSetString("/sim/presets/speed-set", "NED");
753     if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
754         fgSetDouble("/sim/presets/speed-east-fps", atof(arg));
755     else
756         fgSetDouble("/sim/presets/speed-east-fps",
757                     atof(arg) * SG_METER_TO_FEET);
758     return FG_OPTIONS_OK;
759 }
760
761 static int
762 fgOptVDown( const char *arg )
763 {
764     fgSetString("/sim/presets/speed-set", "NED");
765     if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
766         fgSetDouble("/sim/presets/speed-down-fps", atof(arg));
767     else
768         fgSetDouble("/sim/presets/speed-down-fps",
769                             atof(arg) * SG_METER_TO_FEET);
770     return FG_OPTIONS_OK;
771 }
772
773 static int
774 fgOptVc( const char *arg )
775 {
776     // fgSetString("/sim/presets/speed-set", "knots");
777     // fgSetDouble("/velocities/airspeed-kt", atof(arg.substr(5)));
778     fgSetString("/sim/presets/speed-set", "knots");
779     fgSetDouble("/sim/presets/airspeed-kt", atof(arg));
780     return FG_OPTIONS_OK;
781 }
782
783 static int
784 fgOptMach( const char *arg )
785 {
786     fgSetString("/sim/presets/speed-set", "mach");
787     fgSetDouble("/sim/presets/mach", atof(arg));
788     return FG_OPTIONS_OK;
789 }
790
791 static int
792 fgOptRoc( const char *arg )
793 {
794     fgSetDouble("/velocities/vertical-speed-fps", atof(arg)/60);
795     return FG_OPTIONS_OK;
796 }
797
798 static int
799 fgOptFgRoot( const char *arg )
800 {
801     globals->set_fg_root(arg);
802     return FG_OPTIONS_OK;
803 }
804
805 static int
806 fgOptFgScenery( const char *arg )
807 {
808     globals->set_fg_scenery(arg);
809     return FG_OPTIONS_OK;
810 }
811
812 static int
813 fgOptFov( const char *arg )
814 {
815     parse_fov( arg );
816     return FG_OPTIONS_OK;
817 }
818
819 static int
820 fgOptGeometry( const char *arg )
821 {
822     bool geometry_ok = true;
823     int xsize = 0, ysize = 0;
824     string geometry = arg;
825     string::size_type i = geometry.find('x');
826
827     if (i != string::npos) {
828         xsize = atoi(geometry.substr(0, i));
829         ysize = atoi(geometry.substr(i+1));
830     } else {
831         geometry_ok = false;
832     }
833
834     if ( xsize <= 0 || ysize <= 0 ) {
835         xsize = 640;
836         ysize = 480;
837         geometry_ok = false;
838     }
839
840     if ( !geometry_ok ) {
841         SG_LOG( SG_GENERAL, SG_ALERT, "Unknown geometry: " << geometry );
842         SG_LOG( SG_GENERAL, SG_ALERT,
843                 "Setting geometry to " << xsize << 'x' << ysize << '\n');
844     } else {
845         SG_LOG( SG_GENERAL, SG_INFO,
846                 "Setting geometry to " << xsize << 'x' << ysize << '\n');
847         fgSetInt("/sim/startup/xsize", xsize);
848         fgSetInt("/sim/startup/ysize", ysize);
849     }
850     return FG_OPTIONS_OK;
851 }
852
853 static int
854 fgOptBpp( const char *arg )
855 {
856     string bits_per_pix = arg;
857     if ( bits_per_pix == "16" ) {
858         fgSetInt("/sim/rendering/bits-per-pixel", 16);
859     } else if ( bits_per_pix == "24" ) {
860         fgSetInt("/sim/rendering/bits-per-pixel", 24);
861     } else if ( bits_per_pix == "32" ) {
862         fgSetInt("/sim/rendering/bits-per-pixel", 32);
863     } else {
864         SG_LOG(SG_GENERAL, SG_ALERT, "Unsupported bpp " << bits_per_pix);
865     }
866     return FG_OPTIONS_OK;
867 }
868
869 static int
870 fgOptTimeOffset( const char *arg )
871 {
872     fgSetInt("/sim/startup/time-offset",
873                 parse_time_offset( arg ));
874     fgSetString("/sim/startup/time-offset-type", "system-offset");
875     return FG_OPTIONS_OK;
876 }
877
878 static int
879 fgOptStartDateSys( const char *arg )
880 {
881     fgSetInt("/sim/startup/time-offset", parse_date( arg ) );
882     fgSetString("/sim/startup/time-offset-type", "system");
883     return FG_OPTIONS_OK;
884 }
885
886 static int
887 fgOptStartDateLat( const char *arg )
888 {
889     fgSetInt("/sim/startup/time-offset", parse_date( arg ) );
890     fgSetString("/sim/startup/time-offset-type", "latitude");
891     return FG_OPTIONS_OK;
892 }
893
894 static int
895 fgOptStartDateGmt( const char *arg )
896 {
897     fgSetInt("/sim/startup/time-offset", parse_date( arg ) );
898     fgSetString("/sim/startup/time-offset-type", "gmt");
899     return FG_OPTIONS_OK;
900 }
901
902 #ifdef FG_NETWORK_OLK
903 static int
904 fgOptNetHud( const char *arg )
905 {
906     fgSetBool("/sim/hud/net-display", true);
907     net_hud_display = 1;        // FIXME
908     return FG_OPTIONS_OK;
909 }
910 #endif
911
912 static int
913 fgOptTraceRead( const char *arg )
914 {
915     string name = arg;
916     SG_LOG(SG_GENERAL, SG_INFO, "Tracing reads for property " << name);
917     fgGetNode(name.c_str(), true)
918         ->setAttribute(SGPropertyNode::TRACE_READ, true);
919     return FG_OPTIONS_OK;
920 }
921
922 static int
923 fgOptTraceWrite( const char *arg )
924 {
925     string name = arg;
926     SG_LOG(SG_GENERAL, SG_INFO, "Tracing writes for property " << name);
927     fgGetNode(name.c_str(), true)
928         ->setAttribute(SGPropertyNode::TRACE_WRITE, true);
929     return FG_OPTIONS_OK;
930 }
931
932 static int
933 fgOptViewOffset( const char *arg )
934 {
935     // $$$ begin - added VS Renganathan, 14 Oct 2K
936     // for multi-window outside window imagery
937     string woffset = arg;
938     double default_view_offset = 0.0;
939     if ( woffset == "LEFT" ) {
940             default_view_offset = SGD_PI * 0.25;
941     } else if ( woffset == "RIGHT" ) {
942         default_view_offset = SGD_PI * 1.75;
943     } else if ( woffset == "CENTER" ) {
944         default_view_offset = 0.00;
945     } else {
946         default_view_offset = atof( woffset.c_str() ) * SGD_DEGREES_TO_RADIANS;
947     }
948     /* apparently not used (CLO, 11 Jun 2002) 
949         FGViewer *pilot_view =
950             (FGViewer *)globals->get_viewmgr()->get_view( 0 ); */
951     // this will work without calls to the viewer...
952     fgSetDouble( "/sim/current-view/heading-offset-deg",
953                     default_view_offset  * SGD_RADIANS_TO_DEGREES );
954     // $$$ end - added VS Renganathan, 14 Oct 2K
955     return FG_OPTIONS_OK;
956 }
957
958 static int
959 fgOptVisibilityMiles( const char *arg )
960 {
961     double visibility = atof( arg ) * 5280.0 * SG_FEET_TO_METER;
962     fgSetDouble("/environment/visibility-m", visibility);
963     return FG_OPTIONS_OK;
964 }
965
966 static int
967 fgOptRandomWind( const char *arg )
968 {
969     double min_hdg = sg_random() * 360.0;
970     double max_hdg = min_hdg + (20 - sqrt(sg_random() * 400));
971     double speed = 40 - sqrt(sg_random() * 1600.0);
972     double gust = speed + (10 - sqrt(sg_random() * 100));
973     setup_wind(min_hdg, max_hdg, speed, gust);
974     return FG_OPTIONS_OK;
975 }
976
977 static int
978 fgOptWind( const char *arg )
979 {
980     double min_hdg, max_hdg, speed, gust;
981     if (!parse_wind( arg, &min_hdg, &max_hdg, &speed, &gust)) {
982         SG_LOG( SG_GENERAL, SG_ALERT, "bad wind value " << arg );
983         return FG_OPTIONS_ERROR;
984     }
985     setup_wind(min_hdg, max_hdg, speed, gust);
986     return FG_OPTIONS_OK;
987 }
988
989 static int
990 fgOptWp( const char *arg )
991 {
992     parse_wp( arg );
993     return FG_OPTIONS_OK;
994 }
995
996 static int
997 fgOptFlightPlan( const char *arg )
998 {
999     parse_flightplan ( arg );
1000     return FG_OPTIONS_OK;
1001 }
1002
1003 static int
1004 fgOptConfig( const char *arg )
1005 {
1006     string file = arg;
1007     try {
1008         readProperties(file, globals->get_props());
1009     } catch (const sg_exception &e) {
1010         string message = "Error loading config file: ";
1011         message += e.getFormattedMessage();
1012         SG_LOG(SG_INPUT, SG_ALERT, message);
1013         exit(2);
1014     }
1015     return FG_OPTIONS_OK;
1016 }
1017
1018 static map<string,size_t> fgOptionMap;
1019
1020 /*
1021    option       has_param type        property         b_param s_param  func
1022
1023 where:
1024  option    : name of the option
1025  has_param : option is --name=value if true or --name if false
1026  type      : OPTION_BOOL    - property is a boolean
1027              OPTION_STRING  - property is a string
1028              OPTION_DOUBLE  - property is a double
1029              OPTION_INT     - property is an integer
1030              OPTION_CHANNEL - name of option is the name of a channel
1031              OPTION_FUNC    - the option trigger a function
1032  b_param   : if type==OPTION_BOOL,
1033              value set to the property (has_param is false for boolean)
1034  s_param   : if type==OPTION_STRING,
1035              value set to the property if has_param is false
1036  func      : function called if type==OPTION_FUNC. if has_param is true,
1037              the value is passed to the function as a string, otherwise,
1038              0 is passed. 
1039
1040     For OPTION_DOUBLE and OPTION_INT, the parameter value is converted into a
1041     double or an integer and set to the property.
1042
1043     For OPTION_CHANNEL, add_channel is called with the parameter value as the
1044     argument.
1045 */
1046
1047 enum OptionType { OPTION_BOOL, OPTION_STRING, OPTION_DOUBLE, OPTION_INT, OPTION_CHANNEL, OPTION_FUNC };
1048 struct OptionDesc {
1049     char *option;
1050     bool has_param;
1051     enum OptionType type;
1052     char *property;
1053     bool b_param;
1054     char *s_param;
1055     int (*func)( const char * );
1056     } fgOptionArray[] = {
1057        
1058     {"language",                     true,  OPTION_FUNC,   "", false, "", fgOptLanguage },
1059     {"disable-game-mode",            false, OPTION_BOOL,   "/sim/startup/game-mode", false, "", 0 },
1060     {"enable-game-mode",             false, OPTION_BOOL,   "/sim/startup/game-mode", true, "", 0 },
1061     {"disable-splash-screen",        false, OPTION_BOOL,   "/sim/startup/splash-screen", false, "", 0 },
1062     {"enable-splash-screen",         false, OPTION_BOOL,   "/sim/startup/splash-screen", true, "", 0 },
1063     {"disable-intro-music",          false, OPTION_BOOL,   "/sim/startup/intro-music", false, "", 0 },
1064     {"enable-intro-music",           false, OPTION_BOOL,   "/sim/startup/intro-music", true, "", 0 },
1065     {"disable-mouse-pointer",        false, OPTION_STRING, "/sim/startup/mouse-pointer", false, "disabled", 0 },
1066     {"enable-mouse-pointer",         false, OPTION_STRING, "/sim/startup/mouse-pointer", false, "enabled", 0 },
1067     {"disable-random-objects",       false, OPTION_BOOL,   "/sim/rendering/random-objects", false, "", 0 },
1068     {"enable-random-objects",        false, OPTION_BOOL,   "/sim/rendering/random-objects", true, "", 0 },
1069     {"disable-freeze",               false, OPTION_BOOL,   "/sim/freeze/master", false, "", 0 },
1070     {"enable-freeze",                false, OPTION_BOOL,   "/sim/freeze/master", true, "", 0 },
1071     {"disable-fuel-freeze",          false, OPTION_BOOL,   "/sim/freeze/fuel", false, "", 0 },
1072     {"enable-fuel-freeze",           false, OPTION_BOOL,   "/sim/freeze/fuel", true, "", 0 },
1073     {"disable-clock-freeze",         false, OPTION_BOOL,   "/sim/freeze/clock", false, "", 0 },
1074     {"enable-clock-freeze",          false, OPTION_BOOL,   "/sim/freeze/clock", true, "", 0 },
1075     {"disable-anti-alias-hud",       false, OPTION_BOOL,   "/sim/hud/antialiased", false, "", 0 },
1076     {"enable-anti-alias-hud",        false, OPTION_BOOL,   "/sim/hud/antialiased", true, "", 0 },
1077     {"control",                      true,  OPTION_STRING, "/sim/control-mode", false, "", 0 },
1078     {"disable-auto-coordination",    false, OPTION_BOOL,   "/sim/auto-coordination", false, "", 0 },
1079     {"enable-auto-coordination",     false, OPTION_BOOL,   "/sim/auto-coordination", true, "", 0 },
1080     {"browser-app",                  true,  OPTION_STRING, "/sim/startup/browser-app", false, "", 0 },
1081     {"disable-hud",                  false, OPTION_BOOL,   "/sim/hud/visibility", false, "", 0 },
1082     {"enable-hud",                   false, OPTION_BOOL,   "/sim/hud/visibility", true, "", 0 },
1083     {"disable-panel",                false, OPTION_BOOL,   "/sim/panel/visibility", false, "", 0 },
1084     {"enable-panel",                 false, OPTION_BOOL,   "/sim/panel/visibility", true, "", 0 },
1085     {"disable-sound",                false, OPTION_BOOL,   "/sim/sound/audible", false, "", 0 },
1086     {"enable-sound",                 false, OPTION_BOOL,   "/sim/sound/audible", true, "", 0 },
1087     {"airport",                      true,  OPTION_STRING, "/sim/presets/airport-id", false, "", 0 },
1088     {"airport-id",                   true,  OPTION_STRING, "/sim/presets/airport-id", false, "", 0 },
1089     {"runway",                       true,  OPTION_STRING, "/sim/presets/runway", false, "", 0 },
1090     {"vor",                          true,  OPTION_FUNC,   "", false, "", fgOptVOR },
1091     {"ndb",                          true,  OPTION_FUNC,   "", false, "", fgOptNDB },
1092     {"fix",                          true,  OPTION_FUNC,   "", false, "", fgOptFIX },
1093     {"offset-distance",              true,  OPTION_DOUBLE, "/sim/presets/offset-distance", false, "", 0 },
1094     {"offset-azimuth",               true,  OPTION_DOUBLE, "/sim/presets/offset-azimuth", false, "", 0 },
1095     {"lon",                          true,  OPTION_FUNC,   "", false, "", fgOptLon },
1096     {"lat",                          true,  OPTION_FUNC,   "", false, "", fgOptLat },
1097     {"altitude",                     true,  OPTION_FUNC,   "", false, "", fgOptAltitude },
1098     {"uBody",                        true,  OPTION_FUNC,   "", false, "", fgOptUBody },
1099     {"vBody",                        true,  OPTION_FUNC,   "", false, "", fgOptVBody },
1100     {"wBody",                        true,  OPTION_FUNC,   "", false, "", fgOptWBody },
1101     {"vNorth",                       true,  OPTION_FUNC,   "", false, "", fgOptVNorth },
1102     {"vEast",                        true,  OPTION_FUNC,   "", false, "", fgOptVEast },
1103     {"vDown",                        true,  OPTION_FUNC,   "", false, "", fgOptVDown },
1104     {"vc",                           true,  OPTION_FUNC,   "", false, "", fgOptVc },
1105     {"mach",                         true,  OPTION_FUNC,   "", false, "", fgOptMach },
1106     {"heading",                      true,  OPTION_DOUBLE, "/sim/presets/heading-deg", false, "", 0 },
1107     {"roll",                         true,  OPTION_DOUBLE, "/sim/presets/roll-deg", false, "", 0 },
1108     {"pitch",                        true,  OPTION_DOUBLE, "/sim/presets/pitch-deg", false, "", 0 },
1109     {"glideslope",                   true,  OPTION_DOUBLE, "/sim/presets/glideslope-deg", false, "", 0 },
1110     {"roc",                          true,  OPTION_FUNC,   "", false, "", fgOptRoc },
1111     {"fg-root",                      true,  OPTION_FUNC,   "", false, "", fgOptFgRoot },
1112     {"fg-scenery",                   true,  OPTION_FUNC,   "", false, "", fgOptFgScenery },
1113     {"fdm",                          true,  OPTION_STRING, "/sim/flight-model", false, "", 0 },
1114     {"aero",                         true,  OPTION_STRING, "/sim/aero", false, "", 0 },
1115     {"aircraft-dir",                 true,  OPTION_STRING, "/sim/aircraft-dir", false, "", 0 },
1116     {"model-hz",                     true,  OPTION_INT,    "/sim/model-hz", false, "", 0 },
1117     {"speed",                        true,  OPTION_INT,    "/sim/speed-up", false, "", 0 },
1118     {"trim",                         false, OPTION_BOOL,   "/sim/presets/trim", true, "", 0 },
1119     {"notrim",                       false, OPTION_BOOL,   "/sim/presets/trim", false, "", 0 },
1120     {"on-ground",                    false, OPTION_BOOL,   "/sim/presets/onground", true, "", 0 },
1121     {"in-air",                       false, OPTION_BOOL,   "/sim/presets/onground", false, "", 0 },
1122     {"fog-disable",                  false, OPTION_STRING, "/sim/rendering/fog", false, "disabled", 0 },
1123     {"fog-fastest",                  false, OPTION_STRING, "/sim/rendering/fog", false, "fastest", 0 },
1124     {"fog-nicest",                   false, OPTION_STRING, "/sim/fog", false, "nicest", 0 },
1125     {"disable-distance-attenuation", false, OPTION_BOOL,   "/environment/distance-attenuation", false, "", 0 },
1126     {"enable-distance-attenuation",  false, OPTION_BOOL,   "/environment/distance-attenuation", true, "", 0 },
1127     {"disable-clouds",               false, OPTION_BOOL,   "/environment/clouds/status", false, "", 0 },
1128     {"enable-clouds",                false, OPTION_BOOL,   "/environment/clouds/status", true, "", 0 },
1129 #ifdef FG_USE_CLOUDS_3D
1130     {"disable-clouds3d",             false, OPTION_BOOL,   "/sim/rendering/clouds3d", false, "", 0 },
1131     {"enable-clouds3d",              false, OPTION_BOOL,   "/sim/rendering/clouds3d", true, "", 0 },
1132 #endif
1133     {"fov",                          true,  OPTION_FUNC,   "", false, "", fgOptFov },
1134     {"disable-fullscreen",           false, OPTION_BOOL,   "/sim/startup/fullscreen", false, "", 0 },
1135     {"enable-fullscreen",            false, OPTION_BOOL,   "/sim/startup/fullscreen", true, "", 0 },
1136     {"shading-flat",                 false, OPTION_BOOL,   "/sim/rendering/shading", false, "", 0 },
1137     {"shading-smooth",               false, OPTION_BOOL,   "/sim/rendering/shading", true, "", 0 },
1138     {"disable-skyblend",             false, OPTION_BOOL,   "/sim/rendering/skyblend", false, "", 0 },
1139     {"enable-skyblend",              false, OPTION_BOOL,   "/sim/rendering/skyblend", true, "", 0 },
1140     {"disable-textures",             false, OPTION_BOOL,   "/sim/rendering/textures", false, "", 0 },
1141     {"enable-textures",              false, OPTION_BOOL,   "/sim/rendering/textures", true, "", 0 },
1142     {"disable-wireframe",            false, OPTION_BOOL,   "/sim/rendering/wireframe", false, "", 0 },
1143     {"enable-wireframe",             false, OPTION_BOOL,   "/sim/rendering/wireframe", true, "", 0 },
1144     {"geometry",                     true,  OPTION_FUNC,   "", false, "", fgOptGeometry },
1145     {"bpp",                          true,  OPTION_FUNC,   "", false, "", fgOptBpp },
1146     {"units-feet",                   false, OPTION_STRING, "/sim/startup/units", false, "feet", 0 },
1147     {"units-meters",                 false, OPTION_STRING, "/sim/startup/units", false, "meters", 0 },
1148     {"time-offset",                  true,  OPTION_FUNC,   "", false, "", fgOptTimeOffset },
1149     {"time-match-real",              false, OPTION_STRING, "/sim/startup/time-offset-type", false, "system-offset", 0 },
1150     {"time-match-local",             false, OPTION_STRING, "/sim/startup/time-offset-type", false, "latitude-offset", 0 },
1151     {"start-date-sys",               true,  OPTION_FUNC,   "", false, "", fgOptStartDateSys },
1152     {"start-date-lat",               true,  OPTION_FUNC,   "", false, "", fgOptStartDateLat },
1153     {"start-date-gmt",               true,  OPTION_FUNC,   "", false, "", fgOptStartDateGmt },
1154     {"hud-tris",                     false, OPTION_STRING, "/sim/hud/frame-stat-type", false, "tris", 0 },
1155     {"hud-culled",                   false, OPTION_STRING, "/sim/hud/frame-stat-type", false, "culled", 0 },
1156     {"atc610x",                      true,  OPTION_CHANNEL, "", false, "dummy", 0 },
1157     {"atlas",                        true,  OPTION_CHANNEL, "", false, "", 0 },
1158     {"httpd",                        true,  OPTION_CHANNEL, "", false, "", 0 },
1159 #ifdef FG_JPEG_SERVER
1160     {"jpg-httpd",                    true,  OPTION_CHANNEL, "", false, "", 0 },
1161 #endif
1162     {"native",                       true,  OPTION_CHANNEL, "", false, "", 0 },
1163     {"native-ctrls",                 true,  OPTION_CHANNEL, "", false, "", 0 },
1164     {"native-fdm",                   true,  OPTION_CHANNEL, "", false, "", 0 },
1165     {"native-gui",                   true,  OPTION_CHANNEL, "", false, "", 0 },
1166     {"opengc",                       true,  OPTION_CHANNEL, "", false, "", 0 },
1167     {"garmin",                       true,  OPTION_CHANNEL, "", false, "", 0 },
1168     {"nmea",                         true,  OPTION_CHANNEL, "", false, "", 0 },
1169     {"generic",                      true,  OPTION_CHANNEL, "", false, "", 0 },
1170     {"props",                        true,  OPTION_CHANNEL, "", false, "", 0 },
1171     {"telnet",                       true,  OPTION_CHANNEL, "", false, "", 0 },
1172     {"pve",                          true,  OPTION_CHANNEL, "", false, "", 0 },
1173     {"ray",                          true,  OPTION_CHANNEL, "", false, "", 0 },
1174     {"rul",                          true,  OPTION_CHANNEL, "", false, "", 0 },
1175     {"joyclient",                    true,  OPTION_CHANNEL, "", false, "", 0 },
1176 #ifdef FG_NETWORK_OLK
1177     {"disable-network-olk",          false, OPTION_BOOL,   "/sim/networking/olk", false, "", 0 },
1178     {"enable-network-olk",           false, OPTION_BOOL,   "/sim/networking/olk", true, "", 0 },
1179     {"net-hud",                      false, OPTION_FUNC,   "", false, "", fgOptNetHud },
1180     {"net-id",                       true,  OPTION_STRING, "sim/networking/call-sign", false, "", 0 },
1181 #endif
1182 #ifdef FG_MPLAYER_AS
1183     {"callsign",                     true, OPTION_STRING,  "sim/multiplay/callsign", false, "", 0 },
1184     {"multiplay",                    true,  OPTION_CHANNEL, "", false, "", 0 },
1185 #endif
1186     {"trace-read",                   true,  OPTION_FUNC,   "", false, "", fgOptTraceRead },
1187     {"trace-write",                  true,  OPTION_FUNC,   "", false, "", fgOptTraceWrite },
1188     {"log-level",                    true,  OPTION_INT,    "/sim/log-level", false, "", 0 },
1189     {"view-offset",                  true,  OPTION_FUNC,   "", false, "", fgOptViewOffset },
1190     {"visibility",                   true,  OPTION_DOUBLE, "/environment/visibility-m", false, "", 0 },
1191     {"visibility-miles",             true,  OPTION_FUNC,   "", false, "", fgOptVisibilityMiles },
1192     {"random-wind",                  false, OPTION_FUNC,   "", false, "", fgOptRandomWind },
1193     {"wind",                         true,  OPTION_FUNC,   "", false, "", fgOptWind },
1194     {"turbulence",                   true,  OPTION_DOUBLE,  "/environment/turbulence-norm", false, "", 0 },
1195     {"wp",                           true,  OPTION_FUNC,   "", false, "", fgOptWp },
1196     {"flight-plan",                  true,  OPTION_FUNC,   "", false, "", fgOptFlightPlan },
1197     {"config",                       true,  OPTION_FUNC,   "", false, "", fgOptConfig },
1198     {"aircraft",                     true,  OPTION_STRING, "/sim/aircraft", false, "", 0 },
1199     {0}
1200 };
1201
1202
1203 // Parse a single option
1204 static int
1205 parse_option (const string& arg)
1206 {
1207     if ( fgOptionMap.size() == 0 ) {
1208         size_t i = 0;
1209         OptionDesc *pt = &fgOptionArray[ 0 ];
1210         while ( pt->option != 0 ) {
1211             fgOptionMap[ pt->option ] = i;
1212             i += 1;
1213             pt += 1;
1214         }
1215     }
1216
1217     // General Options
1218     if ( (arg == "--help") || (arg == "-h") ) {
1219         // help/usage request
1220         return(FG_OPTIONS_HELP);
1221     } else if ( (arg == "--verbose") || (arg == "-v") ) {
1222         // verbose help/usage request
1223         return(FG_OPTIONS_VERBOSE_HELP);
1224     } else if ( arg.find( "--show-aircraft") == 0) {
1225         return(FG_OPTIONS_SHOW_AIRCRAFT);
1226     } else if ( arg.find( "--prop:" ) == 0 ) {
1227         string assign = arg.substr(7);
1228         string::size_type pos = assign.find('=');
1229         if ( pos == arg.npos || pos == 0 ) {
1230             SG_LOG( SG_GENERAL, SG_ALERT, "Bad property assignment: " << arg );
1231             return FG_OPTIONS_ERROR;
1232         }
1233         string name = assign.substr(0, pos);
1234         string value = assign.substr(pos + 1);
1235         fgSetString(name.c_str(), value.c_str());
1236         // SG_LOG(SG_GENERAL, SG_INFO, "Setting default value of property "
1237         //        << name << " to \"" << value << '"');
1238     } else if ( arg.find( "--" ) == 0 ) {
1239         size_t pos = arg.find( '=' );
1240         string arg_name;
1241         if ( pos == string::npos ) {
1242             arg_name = arg.substr( 2 );
1243         } else {
1244             arg_name = arg.substr( 2, pos - 2 );
1245         }
1246         map<string,size_t>::iterator it = fgOptionMap.find( arg_name );
1247         if ( it != fgOptionMap.end() ) {
1248             OptionDesc *pt = &fgOptionArray[ it->second ];
1249             switch ( pt->type ) {
1250                 case OPTION_BOOL:
1251                     fgSetBool( pt->property, pt->b_param );
1252                     break;
1253                 case OPTION_STRING:
1254                     if ( pt->has_param && pos != string::npos ) {
1255                         fgSetString( pt->property, arg.substr( pos + 1 ).c_str() );
1256                     } else if ( !pt->has_param && pos == string::npos ) {
1257                         fgSetString( pt->property, pt->s_param );
1258                     } else if ( pt->has_param ) {
1259                         SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << arg << "' needs a parameter" );
1260                         return FG_OPTIONS_ERROR;
1261                     } else {
1262                         SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << arg << "' does not have a parameter" );
1263                         return FG_OPTIONS_ERROR;
1264                     }
1265                     break;
1266                 case OPTION_DOUBLE:
1267                     if ( pos != string::npos ) {
1268                         fgSetDouble( pt->property, atof( arg.substr( pos + 1 ) ) );
1269                     } else {
1270                         SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << arg << "' needs a parameter" );
1271                         return FG_OPTIONS_ERROR;
1272                     }
1273                     break;
1274                 case OPTION_INT:
1275                     if ( pos != string::npos ) {
1276                         fgSetInt( pt->property, atoi( arg.substr( pos + 1 ) ) );
1277                     } else {
1278                         SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << arg << "' needs a parameter" );
1279                         return FG_OPTIONS_ERROR;
1280                     }
1281                     break;
1282                 case OPTION_CHANNEL:
1283                     if ( pt->has_param && pos != string::npos ) {
1284                         add_channel( pt->option, arg.substr( pos + 1 ) );
1285                     } else if ( !pt->has_param && pos == string::npos ) {
1286                         add_channel( pt->option, pt->s_param );
1287                     } else if ( pt->has_param ) {
1288                         SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << arg << "' needs a parameter" );
1289                         return FG_OPTIONS_ERROR;
1290                     } else {
1291                         SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << arg << "' does not have a parameter" );
1292                         return FG_OPTIONS_ERROR;
1293                     }
1294                     break;
1295                 case OPTION_FUNC:
1296                     if ( pt->has_param && pos != string::npos ) {
1297                         return pt->func( arg.substr( pos + 1 ).c_str() );
1298                     } else if ( !pt->has_param && pos == string::npos ) {
1299                         return pt->func( 0 );
1300                     } else if ( pt->has_param ) {
1301                         SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << arg << "' needs a parameter" );
1302                         return FG_OPTIONS_ERROR;
1303                     } else {
1304                         SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << arg << "' does not have a parameter" );
1305                         return FG_OPTIONS_ERROR;
1306                     }
1307                     break;
1308             }
1309         } else {
1310             SG_LOG( SG_GENERAL, SG_ALERT, "Unknown option '" << arg << "'" );
1311             return FG_OPTIONS_ERROR;
1312         }
1313     } else {
1314         SG_LOG( SG_GENERAL, SG_ALERT, "Unknown option '" << arg << "'" );
1315         return FG_OPTIONS_ERROR;
1316     }
1317
1318     return FG_OPTIONS_OK;
1319 }
1320
1321
1322 // Parse the command line options
1323 void
1324 fgParseArgs (int argc, char **argv)
1325 {
1326     bool in_options = true;
1327     bool verbose = false;
1328     bool help = false;
1329
1330     SG_LOG(SG_GENERAL, SG_INFO, "Processing command line arguments");
1331
1332     for (int i = 1; i < argc; i++) {
1333         string arg = argv[i];
1334
1335         if (in_options && (arg.find('-') == 0)) {
1336           if (arg == "--") {
1337             in_options = false;
1338           } else {
1339             int result = parse_option(arg);
1340             if ((result == FG_OPTIONS_HELP) || (result == FG_OPTIONS_ERROR))
1341               help = true;
1342
1343             else if (result == FG_OPTIONS_VERBOSE_HELP)
1344               verbose = true;
1345
1346             else if (result == FG_OPTIONS_SHOW_AIRCRAFT) {
1347                fgShowAircraft();
1348                exit(0);
1349             }
1350           }
1351         } else {
1352           in_options = false;
1353           SG_LOG(SG_GENERAL, SG_INFO,
1354                  "Reading command-line property file " << arg);
1355           readProperties(arg, globals->get_props());
1356         }
1357     }
1358
1359     if (help) {
1360        fgUsage(verbose);
1361        exit(0);
1362     }
1363
1364     SG_LOG(SG_GENERAL, SG_INFO, "Finished command line arguments");
1365 }
1366
1367
1368 // Parse config file options
1369 void
1370 fgParseOptions (const string& path) {
1371     sg_gzifstream in( path );
1372     if ( !in.is_open() ) {
1373         return;
1374     }
1375
1376     SG_LOG( SG_GENERAL, SG_INFO, "Processing config file: " << path );
1377
1378     in >> skipcomment;
1379 #ifndef __MWERKS__
1380     while ( ! in.eof() ) {
1381 #else
1382     char c = '\0';
1383     while ( in.get(c) && c != '\0' ) {
1384         in.putback(c);
1385 #endif
1386         string line;
1387
1388 #if defined( macintosh )
1389         getline( in, line, '\r' );
1390 #else
1391         getline( in, line, '\n' );
1392 #endif
1393
1394         // catch extraneous (DOS) line ending character
1395         if ( line[line.length() - 1] < 32 ) {
1396             line = line.substr( 0, line.length()-1 );
1397         }
1398
1399         if ( parse_option( line ) == FG_OPTIONS_ERROR ) {
1400             cerr << endl << "Config file parse error: " << path << " '" 
1401                     << line << "'" << endl;
1402             fgUsage();
1403             exit(-1);
1404         }
1405         in >> skipcomment;
1406     }
1407 }
1408
1409
1410 // Print usage message
1411 void 
1412 fgUsage (bool verbose)
1413 {
1414     SGPropertyNode *locale = globals->get_locale();
1415
1416     SGPropertyNode options_root;
1417
1418     cout << endl;
1419
1420     try {
1421         fgLoadProps("options.xml", &options_root);
1422     } catch (const sg_exception &ex) {
1423         cout << "Unable to read the help file." << endl;
1424         cout << "Make sure the file options.xml is located in the FlightGear base directory," << endl;
1425         cout << "and the location of the base directory is specified by setting $FG_ROOT or" << endl;
1426         cout << "by adding --fg-root=path as a program argument." << endl;
1427         
1428         exit(-1);
1429     }
1430
1431     SGPropertyNode *options = options_root.getNode("options");
1432     if (!options) {
1433         SG_LOG( SG_GENERAL, SG_ALERT,
1434                 "Error reading options.xml: <options> directive not found." );
1435         exit(-1);
1436     }
1437
1438     SGPropertyNode *usage = locale->getNode(options->getStringValue("usage"));
1439     if (usage) {
1440         cout << "Usage: " << usage->getStringValue() << endl;
1441     }
1442
1443     vector<SGPropertyNode_ptr>section = options->getChildren("section");
1444     for (unsigned int j = 0; j < section.size(); j++) {
1445         string msg = "";
1446
1447         vector<SGPropertyNode_ptr>option = section[j]->getChildren("option");
1448         for (unsigned int k = 0; k < option.size(); k++) {
1449
1450             SGPropertyNode *name = option[k]->getNode("name");
1451             SGPropertyNode *short_name = option[k]->getNode("short");
1452             SGPropertyNode *key = option[k]->getNode("key");
1453             SGPropertyNode *arg = option[k]->getNode("arg");
1454             bool brief = option[k]->getNode("brief");
1455
1456             if ((brief || verbose) && name) {
1457                 string tmp = name->getStringValue();
1458
1459                 if (key){
1460                     tmp.append(":");
1461                     tmp.append(key->getStringValue());
1462                 }
1463                 if (arg) {
1464                     tmp.append("=");
1465                     tmp.append(arg->getStringValue());
1466                 }
1467                 if (short_name) {
1468                     tmp.append(", -");
1469                     tmp.append(short_name->getStringValue());
1470                 }
1471
1472                 char cstr[96];
1473                 if (tmp.size() <= 25) {
1474                     snprintf(cstr, 96, "   --%-27s", tmp.c_str());
1475                 } else {
1476                     snprintf(cstr, 96, "\n   --%s\n%32c", tmp.c_str(), ' ');
1477                 }
1478
1479                 // There may be more than one <description> tag assosiated
1480                 // with one option
1481
1482                 msg += cstr;
1483                 vector<SGPropertyNode_ptr>desc =
1484                                           option[k]->getChildren("description");
1485
1486                 if (desc.size() > 0) {
1487                    for ( unsigned int l = 0; l < desc.size(); l++) {
1488
1489                       // There may be more than one translation line.
1490
1491                       string t = desc[l]->getStringValue();
1492                       SGPropertyNode *n = locale->getNode("strings");
1493                       vector<SGPropertyNode_ptr>trans_desc =
1494                                n->getChildren(t.substr(8).c_str());
1495
1496                       for ( unsigned int m = 0; m < trans_desc.size(); m++ ) {
1497                          string t_str = trans_desc[m]->getStringValue();
1498
1499                          if ((m > 0) || ((l > 0) && m == 0)) {
1500                             snprintf(cstr, 96, "%32c", ' ');
1501                             msg += cstr;
1502
1503                          }
1504
1505                          // If the string is too large to fit on the screen,
1506                          // then split it up in several pieces.
1507
1508                          while ( t_str.size() > 47 ) {
1509
1510                             unsigned int m = t_str.rfind(' ', 47);
1511                             msg += t_str.substr(0, m);
1512                             snprintf(cstr, 96, "\n%32c", ' ');
1513                             msg += cstr;
1514
1515                             t_str.erase(t_str.begin(), t_str.begin() + m + 1);
1516                         }
1517                         msg += t_str + '\n';
1518                      }
1519                   }
1520                }
1521             }
1522         }
1523
1524         SGPropertyNode *name =
1525                             locale->getNode(section[j]->getStringValue("name"));
1526
1527         if (!msg.empty() && name) {
1528            cout << endl << name->getStringValue() << ":" << endl;
1529            cout << msg;
1530            msg.erase();
1531         }
1532     }
1533
1534     if ( !verbose ) {
1535         cout << endl;
1536         cout << "For a complete list of options use --help --verbose" << endl;
1537     }
1538 }
1539
1540 // Show available aircraft types
1541 void fgShowAircraft(void) {
1542    vector<string> aircraft;
1543
1544    SGPath path( globals->get_fg_root() );
1545    path.append("Aircraft");
1546
1547    ulDirEnt* dire;
1548    ulDir *dirp;
1549
1550    dirp = ulOpenDir(path.c_str());
1551    if (dirp == NULL) {
1552       cerr << "Unable to open aircraft directory." << endl;
1553       exit(-1);
1554    }
1555
1556    while ((dire = ulReadDir(dirp)) != NULL) {
1557       char *ptr;
1558
1559       if ((ptr = strstr(dire->d_name, "-set.xml")) && ptr[8] == '\0' ) {
1560           SGPath afile = path;
1561           afile.append(dire->d_name);
1562
1563           *ptr = '\0';
1564
1565           SGPropertyNode root;
1566           try {
1567              readProperties(afile.str(), &root);
1568           } catch (...) {
1569              continue;
1570           }
1571
1572           SGPropertyNode *desc = NULL;
1573           SGPropertyNode *node = root.getNode("sim");
1574           if (node) {
1575              desc = node->getNode("description");
1576           }
1577
1578           char cstr[96];
1579           if (strlen(dire->d_name) <= 27)
1580              snprintf(cstr, 96, "   %-27s  %s", dire->d_name,
1581                       (desc) ? desc->getStringValue() : "" );
1582
1583           else
1584              snprintf(cstr, 96, "   %-27s\n%32c%s", dire->d_name, ' ',
1585                       (desc) ? desc->getStringValue() : "" );
1586
1587           aircraft.push_back(cstr);
1588       }
1589    }
1590
1591    sort(aircraft.begin(), aircraft.end());
1592    cout << "Available aircraft:" << endl;
1593    for ( unsigned int i = 0; i < aircraft.size(); i++ ) {
1594        cout << aircraft[i] << endl;
1595    }
1596
1597    aircraft.clear();
1598    ulCloseDir(dirp);
1599 }