<commit_before>#include <common/log.h>
#include <common/pty.h>
#include <common/list.h>
#include <gdb/gdb.h>
#include <gdb/location.h>
#include <gui/gui.h>
#include <user_cmd/cmd.h>
#include <user_cmd/subcmd.hash.h>
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>


/* static variables */
static pty* inferior_term = 0;
static pthread_t tid;


/* static prototypes */
static void* thread_inferior_output(void* arg);


/* global functions */
int cmd_inferior_exec(gdbif* gdb, int argc, char** argv){
	int fd, r;
	const struct user_subcmd_t* scmd;
	gdb_location_t* loc;


	if(argc < 2){
		USER("invalid number of arguments to command \"%s\"\n", argv[0]);
		cmd_inferior_help(1, argv);
		return 0;
	}

	loc = 0;
	scmd = user_subcmd::lookup(argv[1], strlen(argv[1]));

	if(scmd == 0 || scmd->id == SYM){
		if(scmd == 0)	r = gdb->mi_issue_cmd((char*)"file-exec-and-symbols", RC_DONE, 0, "%ss %d", argv + 1, argc - 1);
		else			r = gdb->mi_issue_cmd((char*)"file-symbol-file", RC_DONE, 0, "%ss %d", argv + 2, argc - 2);

		if(r != 0){
			USER("error loading file\n");
			goto end;
		}

		if(gdb->mi_issue_cmd((char*)"file-list-exec-source-file", RC_DONE, (void**)&loc, "") != 0){
			USER("error getting current source file\n");
			goto end;
		}

		ui->win_create(loc->fullname);
		if(scmd == 0)	USER("loaded input file \"%s\"\n", argv[1]);
		else			USER("loaded input file \"%s\"\n", argv[2]);
	}
	else{
		if(scmd->id == BIN){
			if(gdb->mi_issue_cmd((char*)"file-exec-file", RC_DONE, 0, "%ss %d", argv + 2, argc - 2) != 0)
				USER("error loading binary file \"%s\"\n", argv[2]);
			else
				USER("loaded binary file \"%s\"\n", argv[2]);
		}
		else if(scmd->id ==  ARGS){
			TODO("implement arguments with spaces\n");
			if(gdb->mi_issue_cmd((char*)"exec-arguments", RC_DONE, 0, "%ss %d", argv + 2, argc - 2) != 0)
				USER("error setting program arguments\n");
			else
				USER("set program arguments\n");
		}
		else if(scmd->id == TTY){
			/* setup pty */
			if(strcmp(argv[2], "internal") == 0){
				/* setup pty for output redirection */
				// return if inferior tty is already set to internal
				if(inferior_term != 0)
					return 0;
				
				// initialise pty
				inferior_term = new pty;
				if(inferior_term == 0){
					USER("error allocating pseudo terminal for debugee\n");
					return 0;
				}

				// start thread to read from the pty
				if(pthread_create(&tid, 0, thread_inferior_output, 0) != 0){
					USER("error creating thread to read inferior output\n");

					delete inferior_term;
					inferior_term = 0;

					return 0;
				}

				if(gdb->mi_issue_cmd((char*)"inferior-tty-set", RC_DONE, 0, "%s", inferior_term->get_name()) != 0)
					USER("error setting inferior tty\n");
				else
					USER("set inferior tty to internal\n");
			}
			else{<commit_msg>[user-cmd: inferior]<commit_after>#include <common/log.h>
#include <common/pty.h>
#include <common/list.h>
#include <gdb/gdb.h>
#include <gdb/location.h>
#include <gui/gui.h>
#include <user_cmd/cmd.h>
#include <user_cmd/subcmd.hash.h>
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>


/* static variables */
static pty* inferior_term = 0;
static pthread_t tid;


/* static prototypes */
static void* thread_inferior_output(void* arg);


/* global functions */
int cmd_inferior_exec(gdbif* gdb, int argc, char** argv){
	int fd, r;
	const struct user_subcmd_t* scmd;
	gdb_location_t* loc;


	if(argc < 2){
		USER("invalid number of arguments to command \"%s\"\n", argv[0]);
		cmd_inferior_help(1, argv);
		return 0;
	}

	loc = 0;
	scmd = user_subcmd::lookup(argv[1], strlen(argv[1]));

	if(scmd == 0 || scmd->id == SYM){
		if(scmd == 0)	r = gdb->mi_issue_cmd((char*)"file-exec-and-symbols", RC_DONE, 0, "%ss %d", argv + 1, argc - 1);
		else			r = gdb->mi_issue_cmd((char*)"file-symbol-file", RC_DONE, 0, "%ss %d", argv + 2, argc - 2);

		if(r != 0){
			USER("error loading file\n");
			goto end;
		}

		if(gdb->mi_issue_cmd((char*)"file-list-exec-source-file", RC_DONE, (void**)&loc, "") != 0){
			USER("error getting current source file\n");
			goto end;
		}

		ui->win_create(loc->fullname);
		if(scmd == 0)	USER("loaded input file \"%s\"\n", argv[1]);
		else			USER("loaded input file \"%s\"\n", argv[2]);
	}
	else{
		if(scmd->id == BIN){
			if(gdb->mi_issue_cmd((char*)"file-exec-file", RC_DONE, 0, "%ss %d", argv + 2, argc - 2) != 0)
				USER("error loading binary file \"%s\"\n", argv[2]);
			else
				USER("loaded binary file \"%s\"\n", argv[2]);
		}
		else if(scmd->id ==  ARGS){
			TODO("implement arguments with spaces\n");
			if(gdb->mi_issue_cmd((char*)"exec-arguments", RC_DONE, 0, "%ss %d", argv + 2, argc - 2) != 0)
				USER("error setting program arguments\n");
			else
				USER("set program arguments\n");
		}
		else if(scmd->id == TTY){
			/* setup pty */
			if(strcmp(argv[2], "internal") == 0){
				/* setup pty for output redirection */
				// return if inferior tty is already set to internal
				if(inferior_term != 0)
					return 0;
				
				// initialise pty
				inferior_term = new pty;
				if(inferior_term == 0){
					USER("error allocating pseudo terminal for debugee\n");
					return 0;
				}

				// start thread to read from the pty
				if(pthread_create(&tid, 0, thread_inferior_output, 0) != 0){
					USER("error creating thread to read inferior output\n");

					delete inferior_term;
					inferior_term = 0;

					return 0;
				}

				if(gdb->mi_issue_cmd((char*)"inferior-tty-set", RC_DONE, 0, "%s", inferior_term->get_name()) != 0)
					USER("error setting inferior tty\n");
				else
					USER("set inferior tty to internal\n");
			}
			else{
				/* close internal terminal if exists */
				if(inferior_term != 0){
					pthread_cancel(tid);
					pthread_join(tid, 0);

					delete inferior_term;
					inferior_term = 0;
				}

{'fraction_non_alphanumeric': 0.18203925655723466, 'fraction_numerical': 0.015806843842278966, 'mean_word_length': 3.3621212121212123, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3266471', 'n_tokens_mistral': 2601, 'n_tokens_neox': 2190, 'n_words': 546}