<commit_before>/*
 * drivers/console/console.c
 *
 * Copyright (c) 2007-2010  <NAME> <<EMAIL>>
 * official site: http://xboot.org
 *
 * 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, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <configs.h>
#include <default.h>
#include <types.h>
#include <string.h>
#include <charset.h>
#include <malloc.h>
#include <xml.h>
#include <hash.h>
#include <vsprintf.h>
#include <xboot/initcall.h>
#include <xboot/list.h>
#include <xboot/printk.h>
#include <xboot/proc.h>
#include <fs/fsapi.h>
#include <console/console.h>

static struct console * console_stdin = NULL;
static struct console * console_stdout = NULL;

static struct console_list __console_list = {
	.entry = {
		.next	= &(__console_list.entry),
		.prev	= &(__console_list.entry),
	},
};
static struct console_list * console_list = &__console_list;

struct console * search_console(const char *name)
{
	struct console_list * list;
	struct list_head * pos;

	if(!name)
		return NULL;

	for(pos = (&console_list->entry)->next; pos != (&console_list->entry); pos = pos->next)
	{
		list = list_entry(pos, struct console_list, entry);
		if(strcmp((x_s8*)list->console->name, (const x_s8 *)name) == 0)
			return list->console;
	}

	return NULL;
}

x_bool register_console(struct console * console)
{
	struct console_list * list;

	list = malloc(sizeof(struct console_list));
	if(!list || !console)
	{
		free(list);
		return FALSE;
	}

	if(!console->name || (!console->getcode && !console->putcode) || search_console(console->name))
	{
		free(list);
		return FALSE;
	}

	list->console = console;
	list_add(&list->entry, &console_list->entry);

	if((console_stdin == NULL) && (console->getcode))
		console_stdin = console;

	if((console_stdout == NULL) && (console->putcode))
		console_stdout = console;

	return TRUE;
}

x_bool unregister_console(struct console * console)
{
	struct console_list * list;
	struct list_head * pos;

	if(!console || !console->name)
		return FALSE;

	for(pos = (&console_list->entry)->next; pos != (&console_list->entry); pos = pos->next)
	{
		list = list_entry(pos, struct console_list, entry);
		if(list->console == console)
		{
			list_del(pos);
			free(list);

			if(console_stdin == console)
				console_stdin = NULL;

			if(console_stdout == console)
				console_stdout = NULL;

			return TRUE;
		}
	}

	return FALSE;
}

inline struct console * get_stdin(void)
{
	return console_stdin;
}

inline struct console * get_stdout(void)
{
	return console_stdout;
}

x_bool console_stdio_set(const char * in, const char * out)
{
	struct console * stdin, * stdout;

	if(!in || !out)
		return FALSE;

	stdin = search_console(in);
	if(!stdin || !stdin->getcode)
		return FALSE;

	stdout = search_console(out);
	if(!stdout || !stdout->putcode)
		return FALSE;

	console_stdin = stdin;
	console_stdout = stdout;

	return TRUE;
}

x_bool console_stdio_load(char * file)
{
	struct xml * root, * stdin, * stdout;

	root = xml_parse_file(file);
	if(!root || !root->name)
		return FALSE;

	if(strcmp((const x_s8 *)root->name, (const x_s8 *)"console") != 0)
	{
		xml_free(root);
		return FALSE;
	}

	stdin = xml_get(root, "stdin", 0, -1);
	if(! stdin)
	{
		xml_free(root);
		return FALSE;
	}

	stdout = xml_get(root, "stdout", 0, -1);
	if(! stdout)
	{<commit_msg>fix console_stdio_load bug<commit_after>/*
 * drivers/console/console.c
 *
 * Copyright (c) 2007-2010  <NAME> <<EMAIL>>
 * official site: http://xboot.org
 *
 * 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, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <configs.h>
#include <default.h>
#include <types.h>
#include <string.h>
#include <charset.h>
#include <malloc.h>
#include <xml.h>
#include <hash.h>
#include <vsprintf.h>
#include <xboot/initcall.h>
#include <xboot/list.h>
#include <xboot/printk.h>
#include <xboot/proc.h>
#include <fs/fsapi.h>
#include <console/console.h>

static struct console * console_stdin = NULL;
static struct console * console_stdout = NULL;

static struct console_list __console_list = {
	.entry = {
		.next	= &(__console_list.entry),
		.prev	= &(__console_list.entry),
	},
};
static struct console_list * console_list = &__console_list;

struct console * search_console(const char *name)
{
	struct console_list * list;
	struct list_head * pos;

	if(!name)
		return NULL;

	for(pos = (&console_list->entry)->next; pos != (&console_list->entry); pos = pos->next)
	{
		list = list_entry(pos, struct console_list, entry);
		if(strcmp((x_s8*)list->console->name, (const x_s8 *)name) == 0)
			return list->console;
	}

	return NULL;
}

x_bool register_console(struct console * console)
{
	struct console_list * list;

	list = malloc(sizeof(struct console_list));
	if(!list || !console)
	{
		free(list);
		return FALSE;
	}

	if(!console->name || (!console->getcode && !console->putcode) || search_console(console->name))
	{
		free(list);
		return FALSE;
	}

	list->console = console;
	list_add(&list->entry, &console_list->entry);

	if((console_stdin == NULL) && (console->getcode))
		console_stdin = console;

	if((console_stdout == NULL) && (console->putcode))
		console_stdout = console;

	return TRUE;
}

x_bool unregister_console(struct console * console)
{
	struct console_list * list;
	struct list_head * pos;

	if(!console || !console->name)
		return FALSE;

	for(pos = (&console_list->entry)->next; pos != (&console_list->entry); pos = pos->next)
	{
		list = list_entry(pos, struct console_list, entry);
		if(list->console == console)
		{
			list_del(pos);
			free(list);

			if(console_stdin == console)
				console_stdin = NULL;

			if(console_stdout == console)
				console_stdout = NULL;

			return TRUE;
		}
	}

	return FALSE;
}

inline struct console * get_stdin(void)
{
	return console_stdin;
}

inline struct console * get_stdout(void)
{
	return console_stdout;
}

x_bool console_stdio_set(const char * in, const char * out)
{
	struct console * stdin, * stdout;

	if(!in || !out)
		return FALSE;

	stdin = search_console(in);
	if(!stdin || !stdin->getcode)
		return FALSE;

	stdout = search_console(out);
	if(!stdout || !stdout->putcode)
		return FALSE;

	console_stdin = stdin;
	console_stdout = stdout;

	return TRUE;
}

x_bool console_stdio_load(char * file)
{
	struct xml * root, * stdin, * stdout;

	root = xml_parse_file(file);
	if(!root || !root->name)
		return FALSE;

	if(strcmp((const x_s8 *)root->name, (const x_s8 *)"console") != 0)
	{
		xml_free(root);
		return FALSE;
	}

	stdin = xml_get(root, "stdin", -1);
	if(! stdin)
	{
		xml_free(root);
		return FALSE;
	}

	stdout = xml_get(root, "stdout", -1);
	if(! stdout)
	{
{'fraction_non_alphanumeric': 0.13808120917125657, 'fraction_numerical': 0.008197771230946586, 'mean_word_length': 4.215764863059452, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, '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': '3371226', 'n_tokens_mistral': 2854, 'n_tokens_neox': 2676, 'n_words': 752}