<commit_before>/* Kernel.cpp - stalkware(1) kernel implementation
 * stalkware - blah bleh - (C) 2013, <NAME>
 * See README for contact-, COPYING for license information.  */

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

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cerrno>

#include <err.h>

#include "Kernel.h"

Kernel::Kernel()
: facmap_(facarr, facarr + (sizeof facarr / sizeof *facarr)),
  modmap_(),
  cfgmap_(),
  stmap_(),
  kerncfg_(),
  buddies_(),
  spc_(0)
{
}

Kernel::~Kernel()
{
}

void
Kernel::dump()
{
	/*for(map<Module*, map<string, cfgent>*>::const_iterator
			it = cfgmap_.begin(); it != cfgmap_.end(); it++) {
	}*/

	for(map<Module*, vector<vector<string> > >::const_iterator
			it = stmap_.begin(); it != stmap_.end(); it++) {
		fprintf(stderr, "stmap[%s] dump:\n",
				it->first->name().c_str());

		const vector<vector<string> > &vec = it->second;

		for(vector<vector<string> >::const_iterator
				st = vec.begin(); st != vec.end(); st++) {
			fprintf(stderr, "\t{\n");

			const vector<string> &nvec = *st;

			for(vector<string>::const_iterator
					nt = nvec.begin();
					nt != nvec.end(); nt++) {
				fprintf(stderr, "\t\t'%s'\n", nt->c_str());
			}

			fprintf(stderr, "\t}\n");

		}

	}


}

bool
Kernel::run()
{
	char *spc = NULL;

	for(map<string, Module*>::const_iterator it = modmap_.begin();
			it != modmap_.end(); it++) {
		Module *mod = it->second;
		map<string, cfgent> *cfg = cfgmap_[mod];
		mod->init(it->first, *cfg);
	}
	
	for(;;) {
		time_t tnext = time(NULL) + kerncfg_["pollint"].val.lng_;
		for(map<string, Module*>::const_iterator
				it = modmap_.begin(); it != modmap_.end();
				it++) {
			Module *mod = it->second;
			vector<pair<string, string> > res;
			try {
				it->second->check(stmap_[mod], res);
				for(vector<pair<string, string> >::
						const_iterator itt =
						res.begin(); itt !=
						res.end(); itt++) {
					buddies_[itt->first].tlast = time(NULL);
					buddies_[itt->first].plast = mod->pname();
					buddies_[itt->first].mlast = mod->name();
					buddies_[itt->first].ilast = itt->second;
				}
			} catch(std::exception e) {
				warnx("caught exception");
			}
		}

		if (spc_) {
			if (!spc) {
				spc = new char[spc_];
				memset(spc, '\n', spc_-1);
				spc[spc_-1] = '\0';
			} else //dont do it the first time
				puts(spc);
		}

		time_t now = time(NULL);

		for(map<string, buddy>::const_iterator it = buddies_.begin();
				it != buddies_.end(); it++) {
			buddy const& b = it->second;
			printf("%s (%ds ago as '%s' on '%s' using '%s')\n",
					it->first.c_str(), (int)(now-b.tlast),
					b.ilast.c_str(), b.plast.c_str(),
					b.mlast.c_str());
		}

		if (now < tnext)
			sleep(tnext - now);
	}

	return true;
}

void
Kernel::init(string const& stalkrc, int spacing)
{
	spc_ = spacing;
	process_stalkrc(stalkrc);
}

void
Kernel::process_stalkrc(string const& path)
{
	char *line = NULL;
	size_t len = 0;<commit_msg>sort output list by last-seen<commit_after>/* Kernel.cpp - stalkware(1) kernel implementation
 * stalkware - blah bleh - (C) 2013, <NAME>
 * See README for contact-, COPYING for license information.  */

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

#include <deque>

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cerrno>

#include <err.h>

#include "Kernel.h"

using std::deque;

Kernel::Kernel()
: facmap_(facarr, facarr + (sizeof facarr / sizeof *facarr)),
  modmap_(),
  cfgmap_(),
  stmap_(),
  kerncfg_(),
  buddies_(),
  spc_(0)
{
}

Kernel::~Kernel()
{
}

void
Kernel::dump()
{
	/*for(map<Module*, map<string, cfgent>*>::const_iterator
			it = cfgmap_.begin(); it != cfgmap_.end(); it++) {
	}*/

	for(map<Module*, vector<vector<string> > >::const_iterator
			it = stmap_.begin(); it != stmap_.end(); it++) {
		fprintf(stderr, "stmap[%s] dump:\n",
				it->first->name().c_str());

		const vector<vector<string> > &vec = it->second;

		for(vector<vector<string> >::const_iterator
				st = vec.begin(); st != vec.end(); st++) {
			fprintf(stderr, "\t{\n");

			const vector<string> &nvec = *st;

			for(vector<string>::const_iterator
					nt = nvec.begin();
					nt != nvec.end(); nt++) {
				fprintf(stderr, "\t\t'%s'\n", nt->c_str());
			}

			fprintf(stderr, "\t}\n");

		}

	}


}

bool
Kernel::run()
{
	char *spc = NULL;

	for(map<string, Module*>::const_iterator it = modmap_.begin();
			it != modmap_.end(); it++) {
		Module *mod = it->second;
		map<string, cfgent> *cfg = cfgmap_[mod];
		mod->init(it->first, *cfg);
	}
	
	for(;;) {
		time_t tnext = time(NULL) + kerncfg_["pollint"].val.lng_;
		for(map<string, Module*>::const_iterator
				it = modmap_.begin(); it != modmap_.end();
				it++) {
			Module *mod = it->second;
			vector<pair<string, string> > res;
			try {
				it->second->check(stmap_[mod], res);
				for(vector<pair<string, string> >::
						const_iterator itt =
						res.begin(); itt !=
						res.end(); itt++) {
					buddies_[itt->first].tlast = time(NULL);
					buddies_[itt->first].plast = mod->pname();
					buddies_[itt->first].mlast = mod->name();
					buddies_[itt->first].ilast = itt->second;
				}
			} catch(std::exception e) {
				warnx("caught exception");
			}
		}

		if (spc_) {
			if (!spc) {
				spc = new char[spc_];
				memset(spc, '\n', spc_-1);
				spc[spc_-1] = '\0';
			} else //dont do it the first time
				puts(spc);
		}

		time_t now = time(NULL);

		deque<string> unordered;
		deque<string> ordered;
		
		for(map<string, buddy>::const_iterator it = buddies_.begin();
				it != buddies_.end(); it++)
			unordered.push_back(it->first);
	
		while(unordered.size() > 0) {
			time_t tmax = 0;
			string maxkey = "";
			int maxpos = 0;
			int c = 0;
			for(deque<string>::const_iterator
					it = unordered.begin();
					it != unordered.end(); it++) {
				if (maxkey=="" || buddies_[*it].tlast > tmax) {
					tmax = buddies_[*it].tlast;
					maxkey = *it;
					maxpos = c;
				}
				c++;
			}
			
			ordered.push_back(maxkey);
			unordered.erase(unordered.begin() + maxpos);
		}
		for(deque<string>::const_iterator it = ordered.begin();
				it != ordered.end(); it++) {
			buddy const& b = buddies_[*it];
			printf("%s (%ds ago as '%s' on '%s' using '%s')\n",
					it->c_str(), (int)(now-b.tlast),
					b.ilast.c_str(), b.plast.c_str(),
					b.mlast.c_str());
		}

		if (now < tnext)
			sleep(tnext - now);
	}

	return true;
}

void
Kernel::init(string const& stalkrc, int spacing)
{
	spc_ = spacing;
	process_stalkrc(stalkrc);
}

void
Kernel::process_stalkrc(string const& path)
{
	char *line = NULL;
	size_t len = 0;
{'fraction_non_alphanumeric': 0.20393120393120392, 'fraction_numerical': 0.0036855036855036856, 'mean_word_length': 3.3218314532183144, 'pattern_counts': {'":': 0, '<': 62, '<?xml version=': 0, '>': 100, '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': '6909260', 'n_tokens_mistral': 2947, 'n_tokens_neox': 2519, 'n_words': 523}