Fix coverity #29357
[supertux.git] / src / supertux / command_line_arguments.cpp
1 //  SuperTux
2 //  Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "supertux/command_line_arguments.hpp"
18
19 #include <boost/format.hpp>
20 #include <iostream>
21 #include <physfs.h>
22 #include <stdexcept>
23
24 #include "supertux/gameconfig.hpp"
25 #include "supertux/main.hpp"
26 #include "util/gettext.hpp"
27 #include "version.h"
28
29 CommandLineArguments::CommandLineArguments() :
30   m_action(NO_ACTION),
31   m_log_level(LOG_WARNING),
32   datadir(),
33   userdir(),
34   fullscreen_size(),
35   fullscreen_refresh_rate(),
36   window_size(),
37   aspect_size(),
38   use_fullscreen(),
39   video(),
40   show_fps(),
41   sound_enabled(),
42   music_enabled(),
43   console_enabled(),
44   start_level(),
45   enable_script_debugger(),
46   start_demo(),
47   record_demo(),
48   developer_mode()
49 {
50 }
51
52 CommandLineArguments::~CommandLineArguments()
53 {
54 }
55
56 void
57 CommandLineArguments::print_datadir()
58 {
59   // Print the datadir searchpath to stdout, one path per
60   // line. Then exit. Intended for use by the supertux-editor.
61   char **sp;
62   sp = PHYSFS_getSearchPath();
63   if (sp)
64     for (size_t sp_index = 0; sp[sp_index]; sp_index++)
65       std::cout << sp[sp_index] << std::endl;
66   PHYSFS_freeList(sp);
67 }
68
69 void
70 CommandLineArguments::print_help(const char* arg0)
71 {
72   std::cerr
73             << boost::format(_(     "Usage: %s [OPTIONS] [LEVELFILE]")) % arg0 << "\n" << "\n"
74             << _(     "General Options:" ) << "\n"
75             << _(     "  -h, --help                   Show this help message and quit") << "\n"
76             << _(     "  -v, --version                Show SuperTux version and quit") << "\n"
77             << _(     "  --verbose                    Print verbose messages") << "\n"
78             << _(     "  --debug                      Print extra verbose messages") << "\n"
79             << _( "  --print-datadir              Print supertux's primary data directory.") << "\n" << "\n"
80             << _(     "Video Options:") << "\n"
81             << _(     "  -f, --fullscreen             Run in fullscreen mode") << "\n"
82             << _(     "  -w, --window                 Run in window mode") << "\n"
83             << _(     "  -g, --geometry WIDTHxHEIGHT  Run SuperTux in given resolution") << "\n"
84             << _(     "  -a, --aspect WIDTH:HEIGHT    Run SuperTux with given aspect ratio") << "\n"
85             << _(     "  -d, --default                Reset video settings to default values") << "\n"
86             << _(     "  --renderer RENDERER          Use sdl, opengl, or auto to render") << "\n" << "\n"
87             << _(     "Audio Options:") << "\n"
88             << _(     "  --disable-sound              Disable sound effects") << "\n"
89             << _(     "  --disable-music              Disable music") << "\n" << "\n"
90             << _(     "Game Options:") << "\n"
91             << _(     "  --console                    Enable ingame scripting console") << "\n"
92             << _(     "  --noconsole                  Disable ingame scripting console") << "\n"
93             << _(     "  --show-fps                   Display framerate in levels") << "\n"
94             << _(     "  --no-show-fps                Do not display framerate in levels") << "\n"
95             << _(     "  --developer                  Switch on developer feature") << "\n"
96             << _(     "  -s, --debug-scripts          Enable script debugger.") << "\n" << "\n"
97             << _(     "Demo Recording Options:") << "\n"
98             << _(     "  --record-demo FILE LEVEL     Record a demo to FILE") << "\n"
99             << _(     "  --play-demo FILE LEVEL       Play a recorded demo") << "\n" << "\n"
100             << _(     "Directory Options:") << "\n"
101             << _(     "  --datadir DIR                Set the directory for the games datafiles") << "\n"
102             << _(     "  --userdir DIR                Set the directory for user data (savegames, etc.)") << "\n" << "\n"
103             << _(     "Environment variables:") << "\n"
104             << _(     "  SUPERTUX2_USER_DIR           Directory for user data (savegames, etc.)" ) << "\n"
105             << _(     "  SUPERTUX2_DATA_DIR           Directory for the games datafiles" ) << "\n"<< "\n"
106
107
108
109             << std::flush;
110 }
111
112 void
113 CommandLineArguments::print_version()
114 {
115   std::cout << PACKAGE_NAME << " " << PACKAGE_VERSION << std::endl;
116 }
117
118 void
119 CommandLineArguments::parse_args(int argc, char** argv)
120 {
121   for(int i = 1; i < argc; ++i)
122   {
123     std::string arg = argv[i];
124
125     if (arg == "--version" || arg == "-v")
126     {
127       m_action = PRINT_VERSION;
128
129     }
130     else if (arg == "--help" || arg == "-h")
131     {
132       m_action = PRINT_HELP;
133     }
134     else if (arg == "--print-datadir")
135     {
136       m_action = PRINT_DATADIR;
137     }
138     else if (arg == "--debug")
139     {
140       m_log_level = LOG_DEBUG;
141     }
142     else if (arg == "--verbose")
143     {
144       if (m_log_level < LOG_INFO)
145       {
146         m_log_level = LOG_INFO;
147       }
148     }
149     else if (arg == "--datadir")
150     {
151       if (i+1 >= argc)
152       {
153         throw std::runtime_error("Need to specify a directory for --datadir");
154       }
155       else
156       {
157         datadir = argv[++i];
158       }
159     }
160     else if (arg == "--userdir")
161     {
162       if (i+1 >= argc)
163       {
164         throw std::runtime_error("Need to specify a directory for --userdir");
165       }
166       else
167       {
168         userdir = argv[++i];
169       }
170     }
171     else if (arg == "--fullscreen" || arg == "-f")
172     {
173       use_fullscreen = true;
174     }
175     else if (arg == "--default" || arg == "-d")
176     {
177       use_fullscreen = false;
178
179       window_size = Size(1280, 800);
180       fullscreen_size = Size(1280, 800);
181       fullscreen_refresh_rate = 0;
182       aspect_size = Size(0, 0);  // auto detect
183     }
184     else if (arg == "--window" || arg == "-w")
185     {
186       use_fullscreen = false;
187     }
188     else if (arg == "--geometry" || arg == "-g")
189     {
190       i += 1;
191       if (i >= argc)
192       {
193         throw std::runtime_error("Need to specify a size (WIDTHxHEIGHT) for geometry argument");
194       }
195       else
196       {
197         int width, height;
198         if (sscanf(argv[i], "%9dx%9d", &width, &height) != 2)
199         {
200           throw std::runtime_error("Invalid geometry spec, should be WIDTHxHEIGHT");
201         }
202         else
203         {
204           window_size     = Size(width, height);
205           fullscreen_size = Size(width, height);
206           fullscreen_refresh_rate = 0;
207         }
208       }
209     }
210     else if (arg == "--aspect" || arg == "-a")
211     {
212       i += 1;
213       if (i >= argc)
214       {
215         throw std::runtime_error("Need to specify a ratio (WIDTH:HEIGHT) for aspect ratio");
216       }
217       else
218       {
219         int aspect_width  = 0;
220         int aspect_height = 0;
221         if (strcmp(argv[i], "auto") == 0)
222         {
223           aspect_width  = 0;
224           aspect_height = 0;
225         }
226         else if (sscanf(argv[i], "%9d:%9d", &aspect_width, &aspect_height) != 2)
227         {
228           throw std::runtime_error("Invalid aspect spec, should be WIDTH:HEIGHT or auto");
229         }
230         else
231         {
232           float aspect_ratio = static_cast<float>(aspect_width) / static_cast<float>(aspect_height);
233
234           // use aspect ratio to calculate logical resolution
235           if (aspect_ratio > 1) {
236             aspect_size = Size(static_cast<int>(600 * aspect_ratio + 0.5),
237                                          600);
238           } else {
239             aspect_size = Size(600,
240                                          static_cast<int>(600 * 1/aspect_ratio + 0.5));
241           }
242         }
243       }
244     }
245     else if (arg == "--renderer")
246     {
247       i += 1;
248       if (i >= argc)
249       {
250         throw std::runtime_error("Need to specify a renderer for renderer argument");
251       }
252       else
253       {
254         video = VideoSystem::get_video_system(argv[i]);
255       }
256     }
257     else if (arg == "--show-fps")
258     {
259       show_fps = true;
260     }
261     else if (arg == "--no-show-fps")
262     {
263       show_fps = false;
264     }
265     else if (arg == "--developer")
266     {
267       developer_mode = true;
268     }
269     else if (arg == "--console")
270     {
271       console_enabled = true;
272     }
273     else if (arg == "--noconsole")
274     {
275       console_enabled = false;
276     }
277     else if (arg == "--disable-sound" || arg == "--disable-sfx")
278     {
279       sound_enabled = false;
280     }
281     else if (arg == "--disable-music")
282     {
283       music_enabled = false;
284     }
285     else if (arg == "--play-demo")
286     {
287       if (i+1 >= argc)
288       {
289         throw std::runtime_error("Need to specify a demo filename");
290       }
291       else
292       {
293         start_demo = argv[++i];
294       }
295     }
296     else if (arg == "--record-demo")
297     {
298       if (i+1 >= argc)
299       {
300         throw std::runtime_error("Need to specify a demo filename");
301       }
302       else
303       {
304         record_demo = argv[++i];
305       }
306     }
307     else if (arg == "--debug-scripts" || arg == "-s")
308     {
309       enable_script_debugger = true;
310     }
311     else if (arg[0] != '-')
312     {
313       start_level = arg;
314     }
315     else
316     {
317       throw std::runtime_error((boost::format("Unknown option '%1%''. Use --help to see a list of options") % arg).str());
318     }
319   }
320 }
321
322 void
323 CommandLineArguments::merge_into(Config& config)
324 {
325 #define merge_option(x) if (x) { config.x = *x; }
326
327   merge_option(fullscreen_size);
328   merge_option(fullscreen_refresh_rate);
329   merge_option(window_size);
330   merge_option(aspect_size);
331   merge_option(use_fullscreen);
332   merge_option(video);
333   merge_option(show_fps);
334   merge_option(sound_enabled);
335   merge_option(music_enabled);
336   merge_option(console_enabled);
337   merge_option(start_level);
338   merge_option(enable_script_debugger);
339   merge_option(start_demo);
340   merge_option(record_demo);
341   merge_option(developer_mode);
342
343 #undef merge_option
344 }
345
346 /* EOF */