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