<commit_before>/*
 * zdev - Modify and display the persistent configuration of devices
 *
 * Copyright IBM Corp. 2016, 2017
 *
 * s390-tools is free software; you can redistribute it and/or modify
 * it under the terms of the MIT license. See LICENSE for details.
 */

#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/utsname.h>

#include "attrib.h"
#include "device.h"
#include "devtype.h"
#include "export.h"
#include "setting.h"
#include "subtype.h"

struct export_header {
	export_t type;
	config_t config;
	char *type_id;
	char *id;
};

static struct export_header *header_new(const char *config_str,
					const char *type, const char *id)
{
	config_t config;
	struct export_header *hdr;

	if (!str_to_config(config_str, &config))
		return NULL;
	hdr = misc_malloc(sizeof(struct export_header));
	if (!id)
		hdr->type = export_devtype;
	else
		hdr->type = export_device;
	hdr->config = config;
	hdr->type_id = misc_strdup(type);
	if (id)
		hdr->id = misc_strdup(id);

	return hdr;
}

static void header_free(struct export_header *hdr)
{
	free(hdr->type_id);
	free(hdr->id);
	free(hdr);
}

static void write_key_val(FILE *fd, const char *name, const char *value)
{
	char *str;

	str = quote_str(value, 0);
	fprintf(fd, "%s=%s\n", name, str);
	free(str);
}

static bool is_exportable(struct setting *s, config_t config)
{
	struct attrib *a = s->attrib;

	if (!a)
		return true;
	if (a->mandatory)
		return true;
	if (SCOPE_ACTIVE(config)) {
		if (a->unstable && !a->map) {
			/* Skip values that cannot be determined. */
			return false;
		}
		if (!attrib_match_default(s->attrib, s->value)) {
			/* All non-default values should be exported. */
			return true;
		}
	}
	if (SCOPE_PERSISTENT(config) || SCOPE_AUTOCONF(config)) {
		if (setting_is_set(s))
			return true;
	}

	return false;
}

static void write_settings(FILE *fd, struct setting_list *settings,
			   config_t config)
{
	struct setting *s;
	struct strlist_node *v;<commit_msg>zdev: Do not export inacceptable attribute values<commit_after>/*
 * zdev - Modify and display the persistent configuration of devices
 *
 * Copyright IBM Corp. 2016, 2017
 *
 * s390-tools is free software; you can redistribute it and/or modify
 * it under the terms of the MIT license. See LICENSE for details.
 */

#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/utsname.h>

#include "attrib.h"
#include "device.h"
#include "devtype.h"
#include "export.h"
#include "setting.h"
#include "subtype.h"

struct export_header {
	export_t type;
	config_t config;
	char *type_id;
	char *id;
};

static struct export_header *header_new(const char *config_str,
					const char *type, const char *id)
{
	config_t config;
	struct export_header *hdr;

	if (!str_to_config(config_str, &config))
		return NULL;
	hdr = misc_malloc(sizeof(struct export_header));
	if (!id)
		hdr->type = export_devtype;
	else
		hdr->type = export_device;
	hdr->config = config;
	hdr->type_id = misc_strdup(type);
	if (id)
		hdr->id = misc_strdup(id);

	return hdr;
}

static void header_free(struct export_header *hdr)
{
	free(hdr->type_id);
	free(hdr->id);
	free(hdr);
}

static void write_key_val(FILE *fd, const char *name, const char *value)
{
	char *str;

	str = quote_str(value, 0);
	fprintf(fd, "%s=%s\n", name, str);
	free(str);
}

static bool is_exportable(struct setting *s, config_t config)
{
	struct attrib *a = s->attrib;

	if (!a)
		return true;
	if (a->mandatory)
		return true;
	if (SCOPE_ACTIVE(config)) {
		if (a->unstable && !a->map) {
			/* Skip values that cannot be determined. */
			return false;
		}
		if (!attrib_check_value(a, s->value)) {
			/* Skip values that are not acceptable input values. */
			return false;
		}
		if (!attrib_match_default(s->attrib, s->value)) {
			/* All non-default values should be exported. */
			return true;
		}
	}
	if (SCOPE_PERSISTENT(config) || SCOPE_AUTOCONF(config)) {
		if (setting_is_set(s))
			return true;
	}

	return false;
}

static void write_settings(FILE *fd, struct setting_list *settings,
			   config_t config)
{
	struct setting *s;
	struct strlist_node *v;
{'fraction_non_alphanumeric': 0.1354916067146283, 'fraction_numerical': 0.0057553956834532375, 'mean_word_length': 4.111519607843137, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 42, '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': '7512547', 'n_tokens_mistral': 1642, 'n_tokens_neox': 1529, 'n_words': 436}