<commit_before>#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

#include "util.h"
#include "alloc.h"

#define WHERE_FMT "%s:%d:%d"
#define WHERE_ARGS w->fname, w->line, w->chr + 1

enum
{
	colour_black,
	colour_red,
	colour_green,
	colour_orange,
	colour_blue,
	colour_magenta,
	colour_cyan,
	colour_white,

	colour_err  = colour_red,
	colour_warn = colour_orange
};

static const char *const colour_strs[] = {
	"\033[30m",
	"\033[31m",
	"\033[32m",
	"\033[33m",
	"\033[34m",
	"\033[35m",
	"\033[36m",
	"\033[37m",
};

int warning_count = 0;

const char *where_str_r(char buf[WHERE_BUF_SIZ], const struct where *w)
{
	snprintf(buf, WHERE_BUF_SIZ, WHERE_FMT, WHERE_ARGS);
	return buf;
}

const char *where_str(const struct where *w)
{
	static char buf[WHERE_BUF_SIZ];
	return where_str_r(buf, w);
}

struct where *default_where(struct where *w)
{
	if(!w){
		extern const char *current_fname, *current_line_str;
		extern int current_line, current_chr;
		static struct where instead;

		w = &instead;

		instead.fname    = current_fname;
		instead.line     = current_line;
		instead.chr      = current_chr;
		instead.line_str = current_line_str;
	}

	return w;
}

static void warn_show_line(struct where *w)
{
	extern int show_current_line;

	if(show_current_line && w->line_str){
		static int buffed = 0;
		char *line = ustrdup(w->line_str);
		char *p;
		int i;

		if(!buffed){
			setvbuf(stderr, NULL, _IOLBF, 0);
			buffed = 1;
		}

		for(p = line; *p; p++)
			if(*p == '\t')
				*p = ' ';

		fprintf(stderr, "  \"%s\"\n", line);

		for(i = w->chr + 2; i > 0; i--)
			fputc(' ', stderr);
		fputs("^\n", stderr);

		free(line);
	}
}

void vwarn(struct where *w, int err, int show_line, const char *fmt, va_list l)
{
	static enum { f = 0, t = 1, need_init = 2 } is_tty = need_init;

	if(is_tty == need_init)
		is_tty = isatty(2);

	if(is_tty)
		fputs(colour_strs[err ? colour_err : colour_warn], stderr);
<commit_msg>error line output performs initial trim<commit_after>#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

#include "util.h"
#include "alloc.h"

#define WHERE_FMT "%s:%d:%d"
#define WHERE_ARGS w->fname, w->line, w->chr + 1

enum
{
	colour_black,
	colour_red,
	colour_green,
	colour_orange,
	colour_blue,
	colour_magenta,
	colour_cyan,
	colour_white,

	colour_err  = colour_red,
	colour_warn = colour_orange
};

static const char *const colour_strs[] = {
	"\033[30m",
	"\033[31m",
	"\033[32m",
	"\033[33m",
	"\033[34m",
	"\033[35m",
	"\033[36m",
	"\033[37m",
};

int warning_count = 0;

const char *where_str_r(char buf[WHERE_BUF_SIZ], const struct where *w)
{
	snprintf(buf, WHERE_BUF_SIZ, WHERE_FMT, WHERE_ARGS);
	return buf;
}

const char *where_str(const struct where *w)
{
	static char buf[WHERE_BUF_SIZ];
	return where_str_r(buf, w);
}

struct where *default_where(struct where *w)
{
	if(!w){
		extern const char *current_fname, *current_line_str;
		extern int current_line, current_chr;
		static struct where instead;

		w = &instead;

		instead.fname    = current_fname;
		instead.line     = current_line;
		instead.chr      = current_chr;
		instead.line_str = current_line_str;
	}

	return w;
}

static void warn_show_line(struct where *w)
{
	extern int show_current_line;

	if(show_current_line && w->line_str){
		static int buffed = 0;
		char *line = ustrdup(w->line_str);
		char *p, *nonblank;
		int i;

		if(!buffed){
			/* line buffer stderr since we're outputting chars */
			setvbuf(stderr, NULL, _IOLBF, 0);
			buffed = 1;
		}

		nonblank = NULL;
		for(p = line; *p; p++)
			switch(*p){
				case '\t':
					*p = ' ';
				case ' ':
					break;
				default:
					/* trim initial whitespace */
					if(!nonblank)
						nonblank = p;
			}

		if(!nonblank)
			nonblank = line;

		fprintf(stderr, "  \"%s\"\n", nonblank);

		for(i = w->chr + 2 - (nonblank - line); i > 0; i--)
			fputc(' ', stderr);
		fputs("^\n", stderr);

		free(line);
	}
}

void vwarn(struct where *w, int err, int show_line, const char *fmt, va_list l)
{
	static enum { f = 0, t = 1, need_init = 2 } is_tty = need_init;

	if(is_tty == need_init)
		is_tty = isatty(2);

	if(is_tty)
		fputs(colour_strs[err ? colour_err : colour_warn], stderr);

{'fraction_non_alphanumeric': 0.15555555555555556, 'fraction_numerical': 0.023859649122807018, 'mean_word_length': 3.7723214285714284, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 29, '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': '6490027', 'n_tokens_mistral': 1996, 'n_tokens_neox': 1760, 'n_words': 414}