<commit_before>#include <config/config.h>
#include <common/string.h>
#include <common/log.h>
#include <common/list.h>
#include <common/opt.h>
#include <common/map.h>
#include <gui/vim/vimui.h>
#include <gui/vim/cursor.h>
#include <gui/vim/lexer.lex.h>
#include <cmd.hash.h>
#include <parser.tab.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>


/* class implementation */
vimui::vimui(){
	pthread_mutexattr_t attr;


	read_tid = 0;
	nbserver = 0;
	nbclient = 0;
	ostr = 0;
	cwd = opt.vim_cwd;
	cursor_update = true;

	seq_num = 1;
	reply = 0;

	event_lst = 0;
	list_init(event_lst);

	pthread_mutex_init(&buf_mtx, 0);
	pthread_mutex_init(&reply_mtx, 0);
	pthread_mutex_init(&event_mtx, 0);
	pthread_cond_init(&reply_avail, 0);
	pthread_cond_init(&event_avail, 0);

	pthread_mutexattr_init(&attr);
	pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
	pthread_mutex_init(&ui_mtx, &attr);
	pthread_mutexattr_destroy(&attr);
}

vimui::~vimui(){
	pthread_cond_destroy(&reply_avail);
	pthread_mutex_destroy(&reply_mtx);
	pthread_mutex_destroy(&event_mtx);
	pthread_mutex_destroy(&buf_mtx);
	pthread_mutex_destroy(&ui_mtx);

	/* do not destroy 'event_avail', since this can cause vimgdb
	 * to hand in case it has been terminated via SIGINT or SIGTERM
	 */
	//pthread_cond_destroy(&event_avail);

}

int vimui::init(){
	char* s;


	ostr_len = 256;
	ostr = new char[ostr_len];

	if(ostr == 0)
		goto err_0;

	nbserver = new socket(CONFIG_VIM_NB_PORT, 0);

	if(nbserver == 0)
		goto err_1;

	if(nbserver->init_server(TCP) != 0){
		goto err_2;
	}

	nbclient = nbserver->await_client();

	if(nbclient == 0)
		goto err_2;

	nbclient->set_timeout(0);

	if(pthread_create(&read_tid, 0, readline_thread, this) != 0)
		goto err_3;

	thread_name[read_tid] = "vim-readline";

	// wait for vim to finish init
	while(1){
		s = readline();

		if(s == 0)
			return -1;
		
		if(strcmp(s, "init-done") == 0)
			break;
	}

	return 0;

err_3:
	delete nbclient;

err_2:
	delete nbserver;

err_1:
	delete [] ostr;

err_0:
	return -1;
}

void vimui::destroy(){
	vim_event_t* e;

<commit_msg>[fix: vimui 'double free']<commit_after>#include <config/config.h>
#include <common/string.h>
#include <common/log.h>
#include <common/list.h>
#include <common/opt.h>
#include <common/map.h>
#include <gui/vim/vimui.h>
#include <gui/vim/cursor.h>
#include <gui/vim/lexer.lex.h>
#include <cmd.hash.h>
#include <parser.tab.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>


/* class implementation */
vimui::vimui(){
	pthread_mutexattr_t attr;


	read_tid = 0;
	nbserver = 0;
	nbclient = 0;
	ostr = 0;
	cwd = opt.vim_cwd;
	cursor_update = true;

	seq_num = 1;
	reply = 0;

	event_lst = 0;
	list_init(event_lst);

	pthread_mutex_init(&buf_mtx, 0);
	pthread_mutex_init(&reply_mtx, 0);
	pthread_mutex_init(&event_mtx, 0);
	pthread_cond_init(&reply_avail, 0);
	pthread_cond_init(&event_avail, 0);

	pthread_mutexattr_init(&attr);
	pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
	pthread_mutex_init(&ui_mtx, &attr);
	pthread_mutexattr_destroy(&attr);
}

vimui::~vimui(){
	pthread_cond_destroy(&reply_avail);
	pthread_mutex_destroy(&reply_mtx);
	pthread_mutex_destroy(&event_mtx);
	pthread_mutex_destroy(&buf_mtx);
	pthread_mutex_destroy(&ui_mtx);

	/* do not destroy 'event_avail', since this can cause vimgdb
	 * to hand in case it has been terminated via SIGINT or SIGTERM
	 */
	//pthread_cond_destroy(&event_avail);

}

int vimui::init(){
	char* s;


	ostr_len = 256;
	ostr = new char[ostr_len];

	if(ostr == 0)
		goto err_0;

	nbserver = new socket(CONFIG_VIM_NB_PORT, 0);

	if(nbserver == 0)
		goto err_1;

	if(nbserver->init_server(TCP) != 0){
		goto err_2;
	}

	nbclient = nbserver->await_client();

	if(nbclient == 0)
		goto err_2;

	nbclient->set_timeout(0);

	if(pthread_create(&read_tid, 0, readline_thread, this) != 0)
		goto err_3;

	thread_name[read_tid] = "vim-readline";

	// wait for vim to finish init
	while(1){
		s = readline();

		if(s == 0)
			return -1;
		
		if(strcmp(s, "init-done") == 0)
			break;
	}

	return 0;

err_3:
	delete nbclient;
	nbclient = 0;

err_2:
	delete nbserver;
	nbserver = 0;

err_1:
	delete [] ostr;
	ostr = 0;

err_0:
	return -1;
}

void vimui::destroy(){
	vim_event_t* e;


{'fraction_non_alphanumeric': 0.1503314393939394, 'fraction_numerical': 0.018702651515151516, 'mean_word_length': 4.724932249322493, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 37, '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': '6091850', 'n_tokens_mistral': 1950, 'n_tokens_neox': 1790, 'n_words': 353}