<commit_before>#include "pstore/read-write.h"
#include "pstore/builtins.h"
#include "pstore/header.h"
#include "pstore/block.h"
#include "pstore/die.h"

#include <sys/types.h>
#include <inttypes.h>
#include <sys/stat.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>

static void pstore_column__cat(struct pstore_column *self, int fd)
{
	struct pstore_block *block;
	char *s;

	printf("# Column: %s (ID = %" PRIu64 ", type = %d)\n", self->name, self->column_id, self->type);

	block = pstore_block__read(self, fd);

	while ((s = pstore_block__next_value(block)) != NULL) {
		printf("%s\n", s);
	}

	pstore_block__delete(block);
}

static void pstore_table__cat(struct pstore_table *self, int fd)
{
	unsigned long ndx;

	printf("# Table: %s\n", self->name);

	for (ndx = 0; ndx < self->nr_columns; ndx++) {
		struct pstore_column *column = self->columns[ndx];

		pstore_column__cat(column, fd);
	}
}

static void pstore_header__cat(struct pstore_header *self, int fd)
{
	unsigned long ndx;

	for (ndx = 0; ndx < self->nr_tables; ndx++) {
		struct pstore_table *table = self->tables[ndx];

		pstore_table__cat(table, fd);
	}
}

static void usage(void)
{
	printf("\n usage: pstore cat INPUT\n\n");
	exit(EXIT_FAILURE);
}

int cmd_cat(int argc, char *argv[])
{
	struct pstore_header *header;
	int input;

	if (argc != 3)
		usage();

	input = open(argv[2], O_RDONLY|O_LARGEFILE);
	if (input < 0)
		die("open");

	header = pstore_header__read(input);

	pstore_header__cat(header, input);

	pstore_header__delete(header);

	if (close(input) < 0)
		die("close");

	return 0;
}
<commit_msg>builtin-cat: Add a "-q" quiet mode command line option<commit_after>#include "pstore/read-write.h"
#include "pstore/builtins.h"
#include "pstore/header.h"
#include "pstore/block.h"
#include "pstore/die.h"

#include <sys/types.h>
#include <inttypes.h>
#include <sys/stat.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>

static bool quiet_mode;
static char *input_file;

static void pstore_column__cat(struct pstore_column *self, int fd)
{
	struct pstore_block *block;
	char *s;

	printf("# Column: %s (ID = %" PRIu64 ", type = %d)\n", self->name, self->column_id, self->type);

	block = pstore_block__read(self, fd);

	while ((s = pstore_block__next_value(block)) != NULL) {
		if (!quiet_mode)
			 printf("%s\n", s);
	}

	pstore_block__delete(block);
}

static void pstore_table__cat(struct pstore_table *self, int fd)
{
	unsigned long ndx;

	printf("# Table: %s\n", self->name);

	for (ndx = 0; ndx < self->nr_columns; ndx++) {
		struct pstore_column *column = self->columns[ndx];

		pstore_column__cat(column, fd);
	}
}

static void pstore_header__cat(struct pstore_header *self, int fd)
{
	unsigned long ndx;

	for (ndx = 0; ndx < self->nr_tables; ndx++) {
		struct pstore_table *table = self->tables[ndx];

		pstore_table__cat(table, fd);
	}
}

static bool arg_matches(char *arg, const char *prefix)
{
	return strncmp(arg, prefix, strlen(prefix)) == 0;
}

static void parse_args(int argc, char *argv[])
{
	int ndx = 2;

	if (arg_matches(argv[ndx], "-q")) {
		quiet_mode	= true;
		ndx++;
	}

	input_file		= argv[ndx];
}

static void usage(void)
{
	printf("\n usage: pstore cat INPUT\n\n");
	exit(EXIT_FAILURE);
}

int cmd_cat(int argc, char *argv[])
{
	struct pstore_header *header;
	int input;

	if (argc < 3)
		usage();

	parse_args(argc, argv);

	input = open(input_file, O_RDONLY|O_LARGEFILE);
	if (input < 0)
		die("open");

	header = pstore_header__read(input);

	pstore_header__cat(header, input);

	pstore_header__delete(header);

	if (close(input) < 0)
		die("close");

	return 0;
}

{'fraction_non_alphanumeric': 0.16346933261280736, 'fraction_numerical': 0.005133747635774115, 'mean_word_length': 4.73953488372093, 'pattern_counts': {'":': 0, '<': 29, '<?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': '1490555', 'n_tokens_mistral': 1548, 'n_tokens_neox': 1477, 'n_words': 343}