<commit_before>/*
 * kmod-modinfo - query kernel module information using libkmod.
 *
 * Copyright (C) 2011  ProFUSION embedded systems
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <errno.h>
#include <string.h>
#include <limits.h>
#include <sys/utsname.h>
#include <sys/stat.h>
#include "libkmod.h"

#define LOG(fmt, ...) fprintf(stderr, "ERROR: "fmt, ##__VA_ARGS__)

static char separator = '\n';
static const char *field = NULL;

struct param {
	struct param *next;
	const char *name;
	const char *param;
	const char *type;
	int namelen;
	int paramlen;
	int typelen;
};

static struct param *add_param(const char *name, int namelen, const char *param, int paramlen, const char *type, int typelen, struct param **list)
{
	struct param *it;

	for (it = *list; it != NULL; it = it->next) {
		if (it->namelen == namelen &&
			memcmp(it->name, name, namelen) == 0)
			break;
	}

	if (it == NULL) {
		it = malloc(sizeof(struct param));
		if (it == NULL)
			return NULL;
		it->next = *list;
		*list = it;
		it->name = name;
		it->namelen = namelen;
		it->param = NULL;
		it->type = NULL;
		it->paramlen = 0;
		it->typelen = 0;
	}

	if (param != NULL) {
		it->param = param;
		it->paramlen = paramlen;
	}

	if (type != NULL) {
		it->type = type;
		it->typelen = typelen;
	}

	return it;
}

static int modinfo_do(struct kmod_module *mod)
{
	struct kmod_list *l, *list = NULL;
	struct param *params = NULL;
	int err;

	if (field != NULL && strcmp(field, "filename") == 0) {
		printf("%s%c", kmod_module_get_path(mod), separator);
		return 0;
	} else if (field == NULL) {
		printf("%-16s%s%c", "filename:",
		       kmod_module_get_path(mod), separator);
	}

	err = kmod_module_get_info(mod, &list);
	if (err < 0) {
		LOG("Could not get modinfo from '%s': %s\n",
			kmod_module_get_name(mod), strerror(-err));
		return err;
	}

	kmod_list_foreach(l, list) {
		const char *key = kmod_module_info_get_key(l);
		const char *value = kmod_module_info_get_value(l);
		int keylen;

		if (field != NULL) {
			if (strcmp(field, key) != 0)
				continue;
			/* filtered output contains no key, just value */
			printf("%s%c", value, separator);
			continue;
		}

		if (strcmp(key, "parm") == 0 || strcmp(key, "parmtype") == 0) {
			const char *name, *param, *type;
			int namelen, paramlen, typelen;
			struct param *it;
			const char *colon = strchr(value, ':');
			if (colon == NULL) {
				LOG("Found invalid \"%s=%s\": missing ':'\n",
					key, value);
				continue;
			}

			name = value;
			namelen = colon - value;
			if (strcmp(key, "parm") == 0) {
				param = colon + 1;
				paramlen = strlen(param);
				type = NULL;
				typelen = 0;
			} else {
				param = NULL;
				paramlen = 0;
				type = colon + 1;
				typelen = strlen(type);
			}

			it = add_param(name, namelen, param, paramlen,
					type, typelen, &params);
			if (it == NULL) {
				LOG("Out of memory!\n");
				goto end;
			}

			continue;
		}

		if (separator == '\0') {
			printf("%s=%s%c", key, value, separator);
			continue;
		}

		keylen = strlen(key);
		printf("%s:%-*s%s%c", key, 15 - keylen, "", value, separator);
	}

	if (field != NULL)
		goto end;

	while (params != NULL) {
		struct param *p = params;
		params = p->next;

		if (p->param == NULL)
			printf("%-16s%.*s:%.*s%c", "parm:",
			       p->namelen, p->name, p->typelen, p->type,
			       separator);
		else if (p->type != NULL)<commit_msg>kmod-modinfo: -p (-F parm) shows also parmtype in Debian expected formatting.<commit_after>/*
 * kmod-modinfo - query kernel module information using libkmod.
 *
 * Copyright (C) 2011  ProFUSION embedded systems
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <errno.h>
#include <string.h>
#include <limits.h>
#include <sys/utsname.h>
#include <sys/stat.h>
#include "libkmod.h"

#define LOG(fmt, ...) fprintf(stderr, "ERROR: "fmt, ##__VA_ARGS__)

static char separator = '\n';
static const char *field = NULL;

struct param {
	struct param *next;
	const char *name;
	const char *param;
	const char *type;
	int namelen;
	int paramlen;
	int typelen;
};

static struct param *add_param(const char *name, int namelen, const char *param, int paramlen, const char *type, int typelen, struct param **list)
{
	struct param *it;

	for (it = *list; it != NULL; it = it->next) {
		if (it->namelen == namelen &&
			memcmp(it->name, name, namelen) == 0)
			break;
	}

	if (it == NULL) {
		it = malloc(sizeof(struct param));
		if (it == NULL)
			return NULL;
		it->next = *list;
		*list = it;
		it->name = name;
		it->namelen = namelen;
		it->param = NULL;
		it->type = NULL;
		it->paramlen = 0;
		it->typelen = 0;
	}

	if (param != NULL) {
		it->param = param;
		it->paramlen = paramlen;
	}

	if (type != NULL) {
		it->type = type;
		it->typelen = typelen;
	}

	return it;
}

static int process_parm(const char *key, const char *value, struct param **params)
{
	const char *name, *param, *type;
	int namelen, paramlen, typelen;
	struct param *it;
	const char *colon = strchr(value, ':');
	if (colon == NULL) {
		LOG("Found invalid \"%s=%s\": missing ':'\n",
		    key, value);
		return 0;
	}

	name = value;
	namelen = colon - value;
	if (strcmp(key, "parm") == 0) {
		param = colon + 1;
		paramlen = strlen(param);
		type = NULL;
		typelen = 0;
	} else {
		param = NULL;
		paramlen = 0;
		type = colon + 1;
		typelen = strlen(type);
	}

	it = add_param(name, namelen, param, paramlen, type, typelen, params);
	if (it == NULL) {
		LOG("Out of memory!\n");
		return -ENOMEM;
	}

	return 0;
}

static int modinfo_params_do(const struct kmod_list *list)
{
	const struct kmod_list *l;
	struct param *params = NULL;
	int err = 0;

	kmod_list_foreach(l, list) {
		const char *key = kmod_module_info_get_key(l);
		const char *value = kmod_module_info_get_value(l);
		if (strcmp(key, "parm") != 0 &&
		    strcmp(key, "parmtype") != 0)
			continue;

		err = process_parm(key, value, &params);
		if (err < 0)
			goto end;
	}

	while (params != NULL) {
		struct param *p = params;
		params = p->next;

		if (p->param == NULL)
			printf("%.*s: (%.*s)%c",
			       p->namelen, p->name, p->typelen, p->type,
			       separator);
		else if (p->type != NULL)
			printf("%.*s:%.*s (%.*s)%c",
			       p->namelen, p->name,
			       p->paramlen, p->param,
			       p->typelen, p->type,
			       separator);
		else
			printf("%.*s:%.*s%c",
			       p->namelen, p->name,
			       p->paramlen, p->param,
			       separator);

		free(p);
	}

end:
	while (params != NULL) {
		void *tmp = params;
		params = params->next;
		free(tmp);
	}

	return err;
}

static int modinfo_do(struct kmod_module *mod)
{
	struct kmod_list *l, *list = NULL;
	struct param *params = NULL;
	int err;

	if (field != NULL && strcmp(field, "filename") == 0) {
		printf("%s%c", kmod_module_get_path(mod), separator);
		return 0;
	} else if (field == NULL) {
		printf("%-16s%s%c", "filename:",
		       kmod_module_get_path(mod), separator);
	}

	err = kmod_module_get_info(mod, &list);
	if (err < 0) {
		LOG("Could not get modinfo from '%s': %s\n",
			kmod_module_get_name(mod), strerror(-err));
		return err;
	}

	if (field != NULL && strcmp(field, "parm") == 0) {
		err = modinfo_params_do(list);
		goto end;
	}

	kmod_list_foreach(l, list) {
		const char *key = kmod_module_info_get_key(l);
		const char *value = kmod_module_info_get_value(l);
		int keylen;

		if (field != NULL) {
			if (strcmp(field, key) != 0)
				continue;
			/* filtered output contains no key, just value */
			printf("%s%c", value, separator);
			continue;
		}

		if (strcmp(key, "parm") == 0 || strcmp(key, "parmtype") == 0) {
			err = process_parm(key, value, &params);
			if (err < 0)
				goto end;
			continue;
		}

		if (separator == '\0') {
			printf("%s=%s%c", key, value, separator);
			continue;
		}

		keylen = strlen(key);
		printf("%s:%-*s%s%c", key, 15 - keylen, "", value, separator);
	}

	if (field != NULL)
		goto end;

	while (params != NULL) {
		struct param *p = params;
		params = p->next;

		if (p->param == NULL)
			printf("%-16s%.*s:%.*s%c", "parm:",
			       p->namelen, p->name, p->typelen, p->type,
			       separator);
		else if (p->type != NULL)
{'fraction_non_alphanumeric': 0.15967279294592585, 'fraction_numerical': 0.006374163391054924, 'mean_word_length': 3.1821412705464236, 'pattern_counts': {'":': 2, '<': 25, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4783602', 'n_tokens_mistral': 3797, 'n_tokens_neox': 3389, 'n_words': 1046}