<commit_before>// HDPiRun.c

// Author: <NAME>

#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <signal.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <netinet/in.h>

#ifndef SIOCGIFCONF
#include <sys/sockio.h>
#endif

#include "log.h" 
#include "abort.h"

/* This flag controls termination of the main loop. */
volatile sig_atomic_t keepRunning = 1;

/* The signal handler just clears the flag and re-enables itself. */
void catch_term (int sig)
{
	__LOG(__LOG_DEBUG,"SIGTERM caught, shutting down");
	keepRunning = 0;
	signal (sig, catch_term);
}


int main(int argc, char **argv)
{	
	
    /* Establish a handler for SIGTEM signals. */
    signal (SIGTERM, catch_term);
	
	int optchar;
	char *logF = NULL;
	char *hdHomeRunHost;	
	// Parse those args
	opterr = 0;
	__LOG(__LOG_DEBUG,"About to parse args");
	
	// Accepted args
	//	d : debug level
	//  l : log file
	//  h : hostname/IP of HDHomeRun
	while ((optchar = getopt(argc, argv, "d:l:h:")) != -1)
	{
		__LOG(__LOG_TRACE,"Arg: optopt='%c', optchar='%c'",optopt,optchar);
		
		switch(optchar)
		{
			case 'd':
				errno = 0;
				int newDebug = strtol(optarg,NULL,10);
				if (errno != 0)
				{
					__ABORT("Option -%c requires an integer arguement", optopt);
				}
				if (newDebug != __LOG_TRACE &&
					newDebug != __LOG_DEBUG &&
					newDebug != __LOG_WARN &&
					newDebug != __LOG_ERROR &&
					newDebug != __LOG_CRITICAL)
				{
					__ABORT("Debug level must be one of 0, 1, 2, 3, 4 (Trace, Debug, Warn, Error, Critical). Passed '%i'",newDebug);
				}
				__LOG(__LOG_DEBUG,"Debug level set to '%i'",newDebug);
				__logLevel = newDebug;
				break;
			case 'l':
				__LOG(__LOG_DEBUG,"Logfile arg set to '%s'",optarg);
				// To Do: log file opening. This can only be done once we have a safe method of aborting
				break;
			case 'h':
				__LOG(__LOG_DEBUG,"HDHomeRun host set to '%s'",optarg);
				hdHomeRunHost = optarg;
				break;
			case '?':
				__LOG(__LOG_TRACE,"Unkown arg");
				if (optopt == 'l' ||
					optopt == 'd' ||
					optopt == 'h' )
				{
					__ABORT("Option -%c requires an argument.", optopt);
				}
				else
				{
					__ABORT("Unknown arg -%c.", optopt);
				}
			default:
				__LOG(__LOG_TRACE,"Unkown arg");
				__LOG(__LOG_CRITICAL, "Unknown arg -%c.", optopt);
				return 1;
		}
	}
	
	__LOG(__LOG_DEBUG,"Args Parsed");
	
	__LOG(__LOG_MESSAGE,"hdpirun starting up")
	
	__LOG(__LOG_DEBUG,"Checking Tuner Hosts");
	__LOG(__LOG_DEBUG,"Tuner(s) OK");
	
	__LOG(__LOG_DEBUG,"Opening network ports");
	// Create a socket
	int udpServerSocket = socket(AF_INET,SOCK_DGRAM,0);
    if (udpServerSocket < 0)
	{
		__ABORT("Unable to create UDP socket for network commands");
  	}
	// Create a server address struct
	struct sockaddr_in serverAddress;
	bzero((char *) &serverAddress, sizeof(serverAddress));
	serverAddress.sin_port = htons(65001); // ToDo: Take this from hdhomerunlibrary
	serverAddress.sin_family = AF_INET;
	serverAddress.sin_addr.s_addr = INADDR_ANY;
	// Bind the socket
    if (bind(udpServerSocket, (struct sockaddr *) &serverAddress, sizeof(serverAddress)) < 0)
	{
		__ABORT("Cannot bind to port %i for UDP",65001);
	}
	__LOG(__LOG_TRACE,"UDP port %i bound",65001);
	__LOG(__LOG_DEBUG,"Network ports open");
	
	__LOG(__LOG_MESSAGE,"Waiting for network commands");
	while (keepRunning)
	{
		// Wait for a connection to the socket
		listen(udpServerSocket,5);
	}
	__LOG(__LOG_DEBUG,"Shutting down");
	
	__LOG(__LOG_MESSAGE,"hdpirun shut down");
	
	return 0;
}<commit_msg>Move most code to modules<commit_after>/*
   hdpirun.c
   hdpirun

   Main entry point for hdpirun

   Author: <NAME>
   Copyright: Copyright (c) 2016 <NAME>
   License: See LICENSE
*/
#include <signal.h>

#include "log.h" 
#include "abort.h"
#include "commandline.h"
#include "network.h"
#include "homerun.h"

// This flag controls termination of the main loop.
volatile sig_atomic_t keepRunning = 1;

// Signal handler for termination signal. Stops the app
void catch_term (int sig)
{
	switch (sig)
	{
		case SIGTERM:
			LOG(debug,"SIGTERM caught, shutting down");
			break;
		case SIGINT:
			LOG(debug,"SIGINT caught, shutting down");
			break;
		default:
			LOG(debug,"Unexpected signal caught, shutting down");
	}
	keepRunning = 0;
	signal (sig, catch_term);
}

// main entry point for the app
int main(int argc, char **argv)
{	
    // Establish a handler for SIGTEM signals.
    signal(SIGTERM, catch_term);
	// Use the same for SIGINT (ctrl-c)
	signal(SIGINT, catch_term);
	
	// Parse those args
	LOG(debug,"About to parse args");
	
	if (!parseArgs(argc,argv))
	{
		ABORT("Failed to parse args, exiting");
	}
	logLevel = getArgLogLevel();
	logFile = getArgLogFile();
	
	LOG(debug,"Args Parsed");
	
	// OK note startup
	LOG(message,"hdpirun starting up");
	
	// Check the tuner host
	LOG(debug,"Checking Tuner Hosts");
	verifyHomerunTuner(getArgHDHomeRunIP());
	LOG(debug,"Tuner(s) OK");
	
	// Start networking
	LOG(debug,"Opening network ports");
	bindDiscoverPort();
	bindControlPort();
	LOG(debug,"Network ports open");
	
	LOG(message,"Waiting for network commands");
	while (keepRunning)
	{
		receive();
	}
	LOG(debug,"Shutting down");
	
	LOG(message,"hdpirun shut down");
	
	return 0;
}
{'fraction_non_alphanumeric': 0.1391990758567578, 'fraction_numerical': 0.007893723527146708, 'mean_word_length': 3.731329690346084, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, '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': '1012631', 'n_tokens_mistral': 2090, 'n_tokens_neox': 1841, 'n_words': 504}