<commit_before>/*      $Id: lirc_client.c,v 5.4 2000/02/02 20:28:42 columbus Exp $      */

/****************************************************************************
 ** lirc_client.c ***********************************************************
 ****************************************************************************
 *
 * lirc_client - common routines for lircd clients
 *
 * Copyright (C) 1998 <NAME> <<EMAIL>>
 * Copyright (C) 1998 <NAME> <<EMAIL>>
 *
 */ 

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <limits.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <sys/types.h>

#include "lirc_client.h"

/* internal functions */
char *lirc_startupmode(struct lirc_config_entry *first);
void lirc_freeconfigentries(struct lirc_config_entry *first);
void lirc_clearmode(struct lirc_config *config);
char *lirc_execute(struct lirc_config *config,struct lirc_config_entry *scan);
int lirc_iscode(struct lirc_config_entry *scan,char *remote,char *button,
		 int rep);

static int lirc_lircd;
static int lirc_verbose=0;
static char *lirc_prog=NULL;
static char *lirc_buffer=NULL;

void lirc_printf(char *format_str, ...)
{
	va_list ap;  
	
	if(!lirc_verbose) return;
	
	va_start(ap,format_str);
	vfprintf(stderr,format_str,ap);
	va_end(ap);
}

void lirc_perror(const char *s)
{
	if(!lirc_verbose) return;

	perror(s);
}

int lirc_init(char *prog,int verbose)
{
	struct sockaddr_un addr;

	/* connect to lircd */

	if(prog==NULL || lirc_prog!=NULL) return(-1);
	lirc_prog=strdup(prog);
	lirc_verbose=verbose;
	if(lirc_prog==NULL)
	{
		lirc_printf("%s: out of memory\n",lirc_prog);
		return(-1);
	}
	
	addr.sun_family=AF_UNIX;
	strcpy(addr.sun_path,LIRCD);
	lirc_lircd=socket(AF_UNIX,SOCK_STREAM,0);
	if(lirc_lircd==-1)
	{
		lirc_printf("%s: could not open socket\n",lirc_prog);
		lirc_perror(lirc_prog);
		return(-1);
	}
	if(connect(lirc_lircd,(struct sockaddr *)&addr,sizeof(addr))==-1)
	{
		close(lirc_lircd);
		lirc_printf("%s: could not connect to socket\n",lirc_prog);
		lirc_perror(lirc_prog);
		return(-1);
	}
	return(lirc_lircd);
}

int lirc_deinit(void)
{
	if(lirc_prog!=NULL) free(lirc_prog);
	if(lirc_buffer!=NULL) free(lirc_buffer);
	return(close(lirc_lircd));
}

#define LIRC_READ 255

int lirc_readline(char **line,FILE *f)
{
	char *newline,*ret,*enlargeline;
	int len;
	
	newline=(char *) malloc(LIRC_READ+1);
	if(newline==NULL)
	{
		lirc_printf("%s: out of memory\n",lirc_prog);
		return(-1);
	}
	len=0;
	while(1)
	{
		ret=fgets(newline+len,LIRC_READ+1,f);
		if(ret==NULL)
		{
			free(newline);<commit_msg>fixed reinit problem (thanks to <NAME> for the hint)<commit_after>/*      $Id: lirc_client.c,v 5.5 2000/02/19 19:21:59 columbus Exp $      */

/****************************************************************************
 ** lirc_client.c ***********************************************************
 ****************************************************************************
 *
 * lirc_client - common routines for lircd clients
 *
 * Copyright (C) 1998 <NAME> <<EMAIL>>
 * Copyright (C) 1998 <NAME> <<EMAIL>>
 *
 */ 

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <limits.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <sys/types.h>

#include "lirc_client.h"

/* internal functions */
char *lirc_startupmode(struct lirc_config_entry *first);
void lirc_freeconfigentries(struct lirc_config_entry *first);
void lirc_clearmode(struct lirc_config *config);
char *lirc_execute(struct lirc_config *config,struct lirc_config_entry *scan);
int lirc_iscode(struct lirc_config_entry *scan,char *remote,char *button,
		 int rep);

static int lirc_lircd;
static int lirc_verbose=0;
static char *lirc_prog=NULL;
static char *lirc_buffer=NULL;

void lirc_printf(char *format_str, ...)
{
	va_list ap;  
	
	if(!lirc_verbose) return;
	
	va_start(ap,format_str);
	vfprintf(stderr,format_str,ap);
	va_end(ap);
}

void lirc_perror(const char *s)
{
	if(!lirc_verbose) return;

	perror(s);
}

int lirc_init(char *prog,int verbose)
{
	struct sockaddr_un addr;

	/* connect to lircd */

	if(prog==NULL || lirc_prog!=NULL) return(-1);
	lirc_prog=strdup(prog);
	lirc_verbose=verbose;
	if(lirc_prog==NULL)
	{
		lirc_printf("%s: out of memory\n",prog);
		return(-1);
	}
	
	addr.sun_family=AF_UNIX;
	strcpy(addr.sun_path,LIRCD);
	lirc_lircd=socket(AF_UNIX,SOCK_STREAM,0);
	if(lirc_lircd==-1)
	{
		lirc_printf("%s: could not open socket\n",lirc_prog);
		lirc_perror(lirc_prog);
		return(-1);
	}
	if(connect(lirc_lircd,(struct sockaddr *)&addr,sizeof(addr))==-1)
	{
		close(lirc_lircd);
		lirc_printf("%s: could not connect to socket\n",lirc_prog);
		lirc_perror(lirc_prog);
		return(-1);
	}
	return(lirc_lircd);
}

int lirc_deinit(void)
{
	if(lirc_prog!=NULL)
	{
		free(lirc_prog);
		lirc_prog=NULL;
	}
	if(lirc_buffer!=NULL)
	{
		free(lirc_buffer);
		lirc_buffer=NULL;
	}
	return(close(lirc_lircd));
}

#define LIRC_READ 255

int lirc_readline(char **line,FILE *f)
{
	char *newline,*ret,*enlargeline;
	int len;
	
	newline=(char *) malloc(LIRC_READ+1);
	if(newline==NULL)
	{
		lirc_printf("%s: out of memory\n",lirc_prog);
		return(-1);
	}
	len=0;
	while(1)
	{
		ret=fgets(newline+len,LIRC_READ+1,f);
		if(ret==NULL)
		{
			free(newline);
{'fraction_non_alphanumeric': 0.22389153536093806, 'fraction_numerical': 0.014657383657017223, 'mean_word_length': 5.76456009913259, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 40, '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': '1415671', 'n_tokens_mistral': 2444, 'n_tokens_neox': 2165, 'n_words': 383}