@@ -44,6 +44,7 @@ static char *controller;
4444static char * controller_path ;
4545static char * controller_extension ;
4646static char * matlab_path ;
47+ static char * matlab_args ;
4748static char * current_path ;
4849static int nb_controller_arguments ;
4950static int next_argument_index ;
@@ -168,54 +169,60 @@ static bool get_webots_home() {
168169// Gets and stores the path to the latest installed version of Matlab on the system.
169170static bool get_matlab_path () {
170171 struct dirent * directory_entry ; // Pointer for directory entry
172+
171173#ifdef __APPLE__
172174 const char * matlab_directory = "/Applications/" ;
173175 const char * matlab_version_wc = "MATLAB_R20" ;
174- #else
175- const char * matlab_version_wc = "R20" ;
176- #ifdef _WIN32
176+ const char * matlab_exec_suffix = "/bin/matlab" ;
177+ #elif _WIN32
177178 const char * matlab_directory = "C:\\Program Files\\MATLAB\\" ;
179+ const char * matlab_version_wc = "R20" ;
178180 const char * matlab_exec_suffix = "\\bin\\matlab.exe" ;
179- #else // __linux__
181+ #elif __linux__
180182 const char * matlab_directory = "/usr/local/MATLAB/" ;
183+ const char * matlab_version_wc = "R20" ;
181184 const char * matlab_exec_suffix = "/bin/matlab" ;
182- #endif
185+ #else
186+ #error "OS not supported!"
183187#endif
184188
185189 DIR * directory = opendir (matlab_directory );
186- #ifndef __APPLE__
187190 if (directory == NULL ) {
188- fprintf (stderr , "No installation of Matlab available.\n" );
191+ #ifdef __APPLE__
192+ fprintf (stderr , "Could not open Applications folder to search for MATLAB installation. Please specify it manually using "
193+ "the option '--matlab-path'.\n" );
194+ #else
195+ fprintf (stderr , "No installation of MATLAB found. Please specify it manually using the option '--matlab-path'.\n" );
196+ #endif
189197 return false;
190198 }
191- #endif
199+
192200 // Get latest available Matlab version
193201 char * latest_version = NULL ;
194202 while ((directory_entry = readdir (directory )) != NULL ) {
195203 const size_t directory_name_size = strlen (directory_entry -> d_name ) + 1 ;
196204 if (strncmp (matlab_version_wc , directory_entry -> d_name , strlen (matlab_version_wc )) == 0 ) {
197- if (!latest_version )
205+ if (!latest_version ) {
198206 latest_version = malloc (directory_name_size );
199- else if (strcmp (latest_version , directory_entry -> d_name ) < 0 )
200- memset (latest_version , '\0' , directory_name_size );
201- strncpy (latest_version , directory_entry -> d_name , directory_name_size );
207+ strncpy (latest_version , directory_entry -> d_name , directory_name_size );
208+ } else if (strcmp (latest_version , directory_entry -> d_name ) < 0 ) {
209+ char * tmp = realloc (latest_version , directory_name_size );
210+ if (tmp )
211+ latest_version = tmp ;
212+ strncpy (latest_version , directory_entry -> d_name , directory_name_size );
213+ }
202214 }
203215 }
204216 closedir (directory );
205217 if (!latest_version ) {
206- fprintf (stderr , "No installation of Matlab available .\n" );
218+ fprintf (stderr , "No installation of MATLAB found. Please specify it manually using the option '--matlab-path' .\n" );
207219 return false;
208220 }
209221
210- #ifdef __APPLE__
211- const size_t matlab_path_size = snprintf (NULL , 0 , "%s%s" , matlab_directory , latest_version ) + 1 ;
212- matlab_path = malloc (matlab_path_size );
213- sprintf (matlab_path , "%s%s" , matlab_directory , latest_version );
214- #else
215222 const size_t matlab_path_size = snprintf (NULL , 0 , "%s%s%s" , matlab_directory , latest_version , matlab_exec_suffix ) + 1 ;
216223 matlab_path = malloc (matlab_path_size );
217224 sprintf (matlab_path , "%s%s%s" , matlab_directory , latest_version , matlab_exec_suffix );
218- #endif
225+ printf ( "Using the latest available MATLAB instance: %s\n" , matlab_path );
219226
220227 free (latest_version );
221228 return true;
@@ -233,11 +240,12 @@ static void print_options() {
233240 "connect.\n 1234 is used by default, as it is the default port for Webots.\n This setting allows you to connect to a "
234241 "specific instance of Webots if\n there are multiple instances running on the target machine.\n The port of a Webots "
235242 "instance can be set at its launch.\n\n --robot-name=<robot-name>\n Target a specific robot by specifying its name in "
236- "case multiple robots wait\n for an extern controller in the Webots instance.\n\n --matlab-path=<matlab-path>\n For "
237- "MATLAB controllers, this option allows to specify the path to the\n executable of a specific MATLAB version.\n By "
238- "default, the launcher checks in the default MATLAB installation folder.\n See "
239- "https://cyberbotics.com/doc/guide/running-extern-robot-controllers#running-a-matlab-extern-controller\n for more "
240- "information.\n\n --stdout-redirect\n Redirect the stdout of the controller to the Webots console.\n\n "
243+ "case multiple robots wait\n for an extern controller in the Webots instance.\n\n --interactive\n Launch MATLAB "
244+ "in interactive debugging mode.\n See https://cyberbotics.com/doc/guide/matlab#using-the-matlab-desktop for\n more "
245+ "information.\n\n --matlab-path=<matlab-path>\n For MATLAB controllers, this option allows to specify the path to the "
246+ "\n executable of a specific MATLAB version.\n By default, the launcher checks in the default MATLAB installation "
247+ "folder.\n See https://cyberbotics.com/doc/guide/running-extern-robot-controllers#running-a-matlab-extern-controller\n"
248+ " for more information.\n\n --stdout-redirect\n Redirect the stdout of the controller to the Webots console.\n\n "
241249 "--stderr-redirect\n Redirect the stderr of the controller to the Webots console.\n\n" );
242250}
243251
@@ -250,6 +258,7 @@ static bool parse_options(int nb_arguments, char **arguments) {
250258
251259 controller = NULL ;
252260 matlab_path = NULL ;
261+ matlab_args = NULL ;
253262 char * protocol = NULL ;
254263 char * ip_address = NULL ;
255264 char * port = NULL ;
@@ -273,6 +282,14 @@ static bool parse_options(int nb_arguments, char **arguments) {
273282 const size_t robot_name_size = strlen (arguments [i ] + 13 ) + 1 ;
274283 robot_name = malloc (robot_name_size );
275284 memcpy (robot_name , arguments [i ] + 13 , robot_name_size );
285+ } else if (strncmp (arguments [i ], "--interactive" , 13 ) == 0 ) {
286+ #ifdef _WIN32
287+ matlab_args = malloc (strlen ("-wait -r" ) + 1 );
288+ sprintf (matlab_args , "-wait -r" );
289+ #else
290+ matlab_args = malloc (strlen ("-r" ) + 1 );
291+ sprintf (matlab_args , "-r" );
292+ #endif
276293 } else if (strncmp (arguments [i ], "--matlab-path=" , 14 ) == 0 ) {
277294 const size_t matlab_path_size = strlen (arguments [i ] + 14 ) + 1 ;
278295 matlab_path = malloc (matlab_path_size );
@@ -344,11 +361,12 @@ static bool parse_options(int nb_arguments, char **arguments) {
344361
345362 // Show resulting target options to user
346363 const char * location = strncmp (protocol , "tcp" , 3 ) == 0 ? "remote" : "local" ;
347- printf ("The started controller targets a %s instance (%s protocol) of Webots with port number %s." , location , protocol , port );
348- strncmp (protocol , "tcp" , 3 ) == 0 ? printf (" The IP address of the remote Webots instance is '%s'. " , ip_address ) :
349- printf (" " );
350- robot_name ? printf ("Targeting robot '%s'.\n\n" , robot_name ) :
351- printf ("Targeting the only robot waiting for an extern controller.\n\n" );
364+ printf ("\nThe started controller targets a %s instance (%s protocol) of Webots with port number %s." , location , protocol ,
365+ port );
366+ strncmp (protocol , "tcp" , 3 ) == 0 ? printf (" The IP address of the remote Webots instance is '%s'.\n" , ip_address ) :
367+ printf ("\n" );
368+ robot_name ? printf ("Targeting robot '%s'.\n" , robot_name ) :
369+ printf ("Targeting the only robot waiting for an extern controller.\n" );
352370
353371 free (protocol );
354372 free (ip_address );
@@ -723,6 +741,7 @@ static char **add_controller_arguments(char **argv, char **controller_argv, size
723741static void free_memory () {
724742 free (WEBOTS_HOME );
725743 free (matlab_path );
744+ free (matlab_args );
726745 free (current_path );
727746 free (controller );
728747
@@ -885,6 +904,13 @@ int main(int argc, char **argv) {
885904 if (!matlab_path && !get_matlab_path ())
886905 return -1 ;
887906
907+ if (!matlab_args ) {
908+ matlab_args = malloc (strlen ("-batch" ) + 1 );
909+ sprintf (matlab_args , "-batch" );
910+ } else {
911+ printf ("Running MATLAB in interactive mode...\n" );
912+ }
913+
888914#ifdef _WIN32
889915 const char * launcher_path = "\\lib\\controller\\matlab" ;
890916#elif defined __APPLE__
@@ -902,7 +928,7 @@ int main(int argc, char **argv) {
902928 char * * new_argv = NULL ;
903929 new_argv = add_single_argument (new_argv , & current_size , matlab_path );
904930 new_argv = add_single_argument (new_argv , & current_size , matlab_command );
905- new_argv = add_single_argument (new_argv , & current_size , "-batch" );
931+ new_argv = add_single_argument (new_argv , & current_size , matlab_args );
906932 new_argv = add_single_argument (new_argv , & current_size , "launcher" );
907933 if (nb_controller_arguments )
908934 new_argv = add_controller_arguments (new_argv , argv , & current_size , true);
0 commit comments