<commit_before>/*
 * (C)opyright MMVII <NAME> <<EMAIL> dot manea at <EMAIL> dot <EMAIL>>
 * See LICENSE file for license details.
 *
 */

#include "dzen.h"
#include "action.h"

#include <ctype.h>
#include <locale.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>

Dzen dzen = {0};
static int last_cnt = 0;
typedef void sigfunc(int);


static void 
clean_up(void) {
	int i;

	free_event_list();
	if(dzen.font.set)
		XFreeFontSet(dzen.dpy, dzen.font.set);
	else
		XFreeFont(dzen.dpy, dzen.font.xfont);

	XFreePixmap(dzen.dpy, dzen.title_win.drawable);
	if(dzen.slave_win.max_lines) {
		for(i=0; i < dzen.slave_win.max_lines; i++) {
			XFreePixmap(dzen.dpy, dzen.slave_win.drawable[i]);
			XDestroyWindow(dzen.dpy, dzen.slave_win.line[i]);
		}
		free(dzen.slave_win.line);
		XDestroyWindow(dzen.dpy, dzen.slave_win.win);
	}
	XFreeGC(dzen.dpy, dzen.gc);
	XFreeGC(dzen.dpy, dzen.rgc);
	XFreeGC(dzen.dpy, dzen.tgc);
	XDestroyWindow(dzen.dpy, dzen.title_win.win);
	XCloseDisplay(dzen.dpy);
}

static void
catch_sigusr1(int s) {
	(void)s;
	do_action(sigusr1);
}

static void
catch_sigusr2(int s) {
	(void)s;
	do_action(sigusr2);
}

static void
catch_sigterm(int s) {
	(void)s;
	do_action(onexit);
}

static void
catch_alrm(int s) {
	(void)s;
	do_action(onexit);
	clean_up();
	exit(0);
}

static sigfunc *
setup_signal(int signr, sigfunc *shandler) {
	struct sigaction nh, oh;

	nh.sa_handler = shandler;
	sigemptyset(&nh.sa_mask);
	nh.sa_flags = 0;

	if(sigaction(signr, &nh, &oh) < 0)
		return SIG_ERR;

	return NULL;
}

char *rem=NULL;
static int
chomp(char *inbuf, char *outbuf, int start, int len) {
	int i=0;
	int off=start;

	if(rem) {
		strncpy(outbuf, rem, strlen(rem));
		i += strlen(rem);
		free(rem);
		rem = NULL;
	}
	while(off < len) {
		if(i > MAX_LINE_LEN) {
			outbuf[i] = '\0';
			return ++off;
		}
		if(inbuf[off] != '\n') {
			 outbuf[i++] = inbuf[off++];
		} 
		else if(inbuf[off] == '\n') {
			outbuf[i] = '\0';
			return ++off;
		}
	}

	outbuf[i] = '\0';
	rem = estrdup(outbuf);
	return 0;
}

void
free_buffer(void) {
	int i;
	for(i=0; i<dzen.slave_win.tsize; i++) {
		free(dzen.slave_win.tbuf[i]);
		dzen.slave_win.tbuf[i] = NULL;
	}
	dzen.slave_win.tcnt = 
		dzen.slave_win.last_line_vis = 
		last_cnt = 0;<commit_msg>fixed free_buffer(), so that ^cs() will work again<commit_after>/*
 * (C)opyright MMVII <NAME> <rob dot manea at gmail dot com>
 * See LICENSE file for license details.
 *
 */

#include "dzen.h"
#include "action.h"

#include <ctype.h>
#include <locale.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>

Dzen dzen = {0};
static int last_cnt = 0;
typedef void sigfunc(int);


static void 
clean_up(void) {
	int i;

	free_event_list();
	if(dzen.font.set)
		XFreeFontSet(dzen.dpy, dzen.font.set);
	else
		XFreeFont(dzen.dpy, dzen.font.xfont);

	XFreePixmap(dzen.dpy, dzen.title_win.drawable);
	if(dzen.slave_win.max_lines) {
		for(i=0; i < dzen.slave_win.max_lines; i++) {
			XFreePixmap(dzen.dpy, dzen.slave_win.drawable[i]);
			XDestroyWindow(dzen.dpy, dzen.slave_win.line[i]);
		}
		free(dzen.slave_win.line);
		XDestroyWindow(dzen.dpy, dzen.slave_win.win);
	}
	XFreeGC(dzen.dpy, dzen.gc);
	XFreeGC(dzen.dpy, dzen.rgc);
	XFreeGC(dzen.dpy, dzen.tgc);
	XDestroyWindow(dzen.dpy, dzen.title_win.win);
	XCloseDisplay(dzen.dpy);
}

static void
catch_sigusr1(int s) {
	(void)s;
	do_action(sigusr1);
}

static void
catch_sigusr2(int s) {
	(void)s;
	do_action(sigusr2);
}

static void
catch_sigterm(int s) {
	(void)s;
	do_action(onexit);
}

static void
catch_alrm(int s) {
	(void)s;
	do_action(onexit);
	clean_up();
	exit(0);
}

static sigfunc *
setup_signal(int signr, sigfunc *shandler) {
	struct sigaction nh, oh;

	nh.sa_handler = shandler;
	sigemptyset(&nh.sa_mask);
	nh.sa_flags = 0;

	if(sigaction(signr, &nh, &oh) < 0)
		return SIG_ERR;

	return NULL;
}

char *rem=NULL;
static int
chomp(char *inbuf, char *outbuf, int start, int len) {
	int i=0;
	int off=start;

	if(rem) {
		strncpy(outbuf, rem, strlen(rem));
		i += strlen(rem);
		free(rem);
		rem = NULL;
	}
	while(off < len) {
		if(i > MAX_LINE_LEN) {
			outbuf[i] = '\0';
			return ++off;
		}
		if(inbuf[off] != '\n') {
			 outbuf[i++] = inbuf[off++];
		} 
		else if(inbuf[off] == '\n') {
			outbuf[i] = '\0';
			return ++off;
		}
	}

	outbuf[i] = '\0';
	rem = estrdup(outbuf);
	return 0;
}

void
free_buffer(void) {
	int i;
	for(i=0; i<dzen.slave_win.tcnt; i++) {
		free(dzen.slave_win.tbuf[i]);
		dzen.slave_win.tbuf[i] = NULL;
	}
	dzen.slave_win.tcnt = 
		dzen.slave_win.last_line_vis = 
		last_cnt = 0;
{'fraction_non_alphanumeric': 0.1770573566084788, 'fraction_numerical': 0.007065669160432253, 'mean_word_length': 4.131130063965885, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 36, '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': '4270699', 'n_tokens_mistral': 2339, 'n_tokens_neox': 2207, 'n_words': 412}