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