1919 along with this program. If not, see <http://www.gnu.org/licenses/>. */
2020
2121#include "defs.h"
22+ #include "exceptions.h"
2223#include "arch-utils.h"
2324#include "readline/readline.h"
2425#include "readline/tilde.h"
@@ -47,6 +48,8 @@ extern void disconnect_or_stop_tracing (int from_tty);
4748#include "cli/cli-setshow.h"
4849#include "cli/cli-cmds.h"
4950
51+ #include "python/python.h"
52+
5053#ifdef TUI
5154#include "tui/tui.h" /* For tui_active et.al. */
5255#endif
@@ -187,6 +190,21 @@ struct cmd_list_element *showchecklist;
187190int source_verbose = 0 ;
188191int trace_commands = 0 ;
189192
193+ /* 'script-extension' option support. */
194+
195+ static const char script_ext_off [] = "off" ;
196+ static const char script_ext_soft [] = "soft" ;
197+ static const char script_ext_strict [] = "strict" ;
198+
199+ static const char * script_ext_enums [] = {
200+ script_ext_off ,
201+ script_ext_soft ,
202+ script_ext_strict ,
203+ NULL
204+ };
205+
206+ static const char * script_ext_mode = script_ext_soft ;
207+
190208/* Utility used everywhere when at least one argument is needed and
191209 none is supplied. */
192210
@@ -441,18 +459,25 @@ cd_command (char *dir, int from_tty)
441459 pwd_command ((char * ) 0 , 1 );
442460}
443461
444- void
445- source_script (char * file , int from_tty )
462+ /* Show the current value of the 'script-extension' option. */
463+
464+ static void
465+ show_script_ext_mode (struct ui_file * file , int from_tty ,
466+ struct cmd_list_element * c , const char * value )
446467{
447- FILE * stream ;
448- struct cleanup * old_cleanups ;
468+ fprintf_filtered (file , _ ("\
469+ Script filename extension recognition is \"%s\".\n" ),
470+ value );
471+ }
472+
473+ static int
474+ find_and_open_script (int from_tty , char * * filep , FILE * * streamp ,
475+ struct cleanup * * cleanupp )
476+ {
477+ char * file = * filep ;
449478 char * full_pathname = NULL ;
450479 int fd ;
451-
452- if (file == NULL || * file == 0 )
453- {
454- error (_ ("source command requires file name of file to source." ));
455- }
480+ struct cleanup * old_cleanups ;
456481
457482 file = tilde_expand (file );
458483 old_cleanups = make_cleanup (xfree , file );
@@ -476,12 +501,58 @@ source_script (char *file, int from_tty)
476501 else
477502 {
478503 do_cleanups (old_cleanups );
479- return ;
504+ return 0 ;
480505 }
481506 }
482507
483- stream = fdopen (fd , FOPEN_RT );
484- script_from_file (stream , file );
508+ * streamp = fdopen (fd , FOPEN_RT );
509+ * filep = file ;
510+ * cleanupp = old_cleanups ;
511+
512+ return 1 ;
513+ }
514+
515+ void
516+ source_script (char * file , int from_tty )
517+ {
518+ FILE * stream ;
519+ struct cleanup * old_cleanups ;
520+
521+ if (file == NULL || * file == 0 )
522+ {
523+ error (_ ("source command requires file name of file to source." ));
524+ }
525+
526+ if (!find_and_open_script (from_tty , & file , & stream , & old_cleanups ))
527+ return ;
528+
529+ if (script_ext_mode != script_ext_off
530+ && strlen (file ) > 3 && !strcmp (& file [strlen (file ) - 3 ], ".py" ))
531+ {
532+ volatile struct gdb_exception e ;
533+
534+ TRY_CATCH (e , RETURN_MASK_ERROR )
535+ {
536+ source_python_script (stream , file );
537+ }
538+ if (e .reason < 0 )
539+ {
540+ /* Should we fallback to ye olde GDB script mode? */
541+ if (script_ext_mode == script_ext_soft
542+ && e .reason == RETURN_ERROR && e .error == UNSUPPORTED_ERROR )
543+ {
544+ if (!find_and_open_script (from_tty , & file , & stream , & old_cleanups ))
545+ return ;
546+
547+ script_from_file (stream , file );
548+ }
549+ else
550+ /* Nope, just punt. */
551+ throw_exception (e );
552+ }
553+ }
554+ else
555+ script_from_file (stream , file );
485556
486557 do_cleanups (old_cleanups );
487558}
@@ -1314,6 +1385,19 @@ when GDB is started."), gdbinit);
13141385 source_help_text , & cmdlist );
13151386 set_cmd_completer (c , filename_completer );
13161387
1388+ add_setshow_enum_cmd ("script-extension" , class_support ,
1389+ script_ext_enums , & script_ext_mode , _ ("\
1390+ Set mode for script filename extension recognition." ), _ ("\
1391+ Show mode for script filename extension recognition." ), _ ("\
1392+ off == no filename extension recognition (all sourced files are GDB scripts)\n\
1393+ soft == evaluate script according to filename extension, fallback to GDB script"
1394+ "\n\
1395+ strict == evaluate script according to filename extension, error if not supported"
1396+ ),
1397+ NULL ,
1398+ show_script_ext_mode ,
1399+ & setlist , & showlist );
1400+
13171401 add_com ("quit" , class_support , quit_command , _ ("Exit gdb." ));
13181402 c = add_com ("help" , class_support , help_command ,
13191403 _ ("Print list of commands." ));
0 commit comments