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