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

#include <ctype.h>
#include <err.h>
#include <locale.h>
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <termios.h>
#include <unistd.h>
#include <wchar.h>

#ifdef HAVE_NCURSESW_H
#include <ncursesw/curses.h>
#include <ncursesw/term.h>
#else
#include <curses.h>
#include <term.h>
#endif

#include "compat.h"

#define EX_SIG 128
#define EX_SIGINT (EX_SIG + SIGINT)

#define tty_putp(capability) do { \
	if (tputs(capability, 1, tty_putc) == ERR) \
		errx(1, #capability ": unknown terminfo capability"); \
	} while (0)

#define ESCAPE 27

enum {
	UNKNOWN,
	ALT_ENTER,
	BACKSPACE,
	DEL,
	ENTER,
	CTRL_A,
	CTRL_E,
	CTRL_K,
	CTRL_U,
	CTRL_W,
	UP,
	RIGHT,
	DOWN,
	LEFT,
	PAGE_DOWN,
	PAGE_UP
};

struct choice {
	char	*description;
	char	*string;
	size_t	 length;
	ssize_t	 match_start;	/* inclusive match start offset */
	ssize_t	 match_end;	/* exclusive match end offset */
	float	 score;
};

static int			 choicecmp(const void *, const void *);
static void			 delete_between(char *, size_t, size_t, size_t);
static char			*eager_strpbrk(const char *, const char *);
static void			 filter_choices(void);
static void			 get_choices(void);
static int			 get_key(char *, size_t, size_t *);
static void			 handle_sigint(int);
static int			 isu8cont(unsigned char);
static int			 isu8start(unsigned char);
static int			 min_match(const char *, size_t, ssize_t *,
				    ssize_t *);
static int			 print_choices(int, int);
static void			 print_line(const char *, size_t, int, ssize_t,
				    ssize_t);
static void			 score(struct choice *);
static const struct choice	*selected_choice(void);
static char			*strcasechr(const char *, const char *);
static int			 tty_getc(void);
static void			 tty_init(void);
static int			 tty_putc(int);
static void			 tty_restore(void);
__dead static void		 usage(void);
__dead static void		 version(void);

static struct termios	 original_attributes;
static struct {
	size_t size;
	size_t length;
	struct choice *v;
}			 choices;
static FILE		*tty_in, *tty_out;
static char		*query;
static size_t		 query_size;
static int		 descriptions;
static int		 sort = 1;
static int		 use_alternate_screen = 1;

int
main(int argc, char **argv)
{
	const struct choice	*choice;
	int			 option;
	int			 output_description = 0;

#ifdef HAVE_PLEDGE
	if (pledge("stdio tty rpath wpath cpath", NULL) == -1)
		err(1, "pledge");
#endif

	setlocale(LC_CTYPE, "");

	while ((option = getopt(argc, argv, "dhoq:SvxX")) != -1) {
		switch (option) {
		case 'd':
			descriptions = 1;
			break;
		case 'o':
			/*
			 * Only output description if descriptions are read and
			 * displayed in the list of choices.
			 */
			output_description = descriptions;
			break;
		case 'q':
			if ((query = strdup(optarg)) == NULL)
				err(1, "strdup");
			query_size = strlen(query) + 1;
			break;
		case 'S':
			sort = 0;
			break;
		case 'v':
			version();
		case 'x':<commit_msg>Rename variable option -> c<commit_after>#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <ctype.h>
#include <err.h>
#include <locale.h>
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <termios.h>
#include <unistd.h>
#include <wchar.h>

#ifdef HAVE_NCURSESW_H
#include <ncursesw/curses.h>
#include <ncursesw/term.h>
#else
#include <curses.h>
#include <term.h>
#endif

#include "compat.h"

#define EX_SIG 128
#define EX_SIGINT (EX_SIG + SIGINT)

#define tty_putp(capability) do { \
	if (tputs(capability, 1, tty_putc) == ERR) \
		errx(1, #capability ": unknown terminfo capability"); \
	} while (0)

#define ESCAPE 27

enum {
	UNKNOWN,
	ALT_ENTER,
	BACKSPACE,
	DEL,
	ENTER,
	CTRL_A,
	CTRL_E,
	CTRL_K,
	CTRL_U,
	CTRL_W,
	UP,
	RIGHT,
	DOWN,
	LEFT,
	PAGE_DOWN,
	PAGE_UP
};

struct choice {
	char	*description;
	char	*string;
	size_t	 length;
	ssize_t	 match_start;	/* inclusive match start offset */
	ssize_t	 match_end;	/* exclusive match end offset */
	float	 score;
};

static int			 choicecmp(const void *, const void *);
static void			 delete_between(char *, size_t, size_t, size_t);
static char			*eager_strpbrk(const char *, const char *);
static void			 filter_choices(void);
static void			 get_choices(void);
static int			 get_key(char *, size_t, size_t *);
static void			 handle_sigint(int);
static int			 isu8cont(unsigned char);
static int			 isu8start(unsigned char);
static int			 min_match(const char *, size_t, ssize_t *,
				    ssize_t *);
static int			 print_choices(int, int);
static void			 print_line(const char *, size_t, int, ssize_t,
				    ssize_t);
static void			 score(struct choice *);
static const struct choice	*selected_choice(void);
static char			*strcasechr(const char *, const char *);
static int			 tty_getc(void);
static void			 tty_init(void);
static int			 tty_putc(int);
static void			 tty_restore(void);
__dead static void		 usage(void);
__dead static void		 version(void);

static struct termios	 original_attributes;
static struct {
	size_t size;
	size_t length;
	struct choice *v;
}			 choices;
static FILE		*tty_in, *tty_out;
static char		*query;
static size_t		 query_size;
static int		 descriptions;
static int		 sort = 1;
static int		 use_alternate_screen = 1;

int
main(int argc, char **argv)
{
	const struct choice	*choice;
	int			 c;
	int			 output_description = 0;

#ifdef HAVE_PLEDGE
	if (pledge("stdio tty rpath wpath cpath", NULL) == -1)
		err(1, "pledge");
#endif

	setlocale(LC_CTYPE, "");

	while ((c = getopt(argc, argv, "dhoq:SvxX")) != -1) {
		switch (c) {
		case 'd':
			descriptions = 1;
			break;
		case 'o':
			/*
			 * Only output description if descriptions are read and
			 * displayed in the list of choices.
			 */
			output_description = descriptions;
			break;
		case 'q':
			if ((query = strdup(optarg)) == NULL)
				err(1, "strdup");
			query_size = strlen(query) + 1;
			break;
		case 'S':
			sort = 0;
			break;
		case 'v':
			version();
		case 'x':
{'fraction_non_alphanumeric': 0.12618296529968454, 'fraction_numerical': 0.006641208699983397, 'mean_word_length': 3.897560975609756, 'pattern_counts': {'":': 2, '<': 35, '<?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': '5339450', 'n_tokens_mistral': 2504, 'n_tokens_neox': 2261, 'n_words': 638}