/*
 * callbacks.c
 * 
 * Copyright (C) 2006  Josef Kufner <jk@myserver.cz>
 * 
 * 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.
 *
 */

#include <stdlib.h>
#include <string.h>

#include "callbacks.h"


gboolean stdin_data_cb(GIOChannel *source, GIOCondition condition, gpointer data)
{
	GIOStatus ret;
	GError *err = NULL;
	const int msg_size = 1024;
	char msg[msg_size];
	gsize len;

	print_msg(D_DEBUG, "stdin_data_cb...");
	
	if (condition & G_IO_IN) {
		/* read data */
		ret = g_io_channel_read_chars (source, msg, msg_size - 1, &len, &err);
		msg[len] = 0;
		if (ret == G_IO_STATUS_ERROR) {
			print_msg(D_DEBUG, "reading: %s", err->message);
			g_main_loop_quit(main_loop);
			return 0;
		}

		/* send message */
		print_msg(D_DEBUG, "Read %u bytes: %s", len, msg);
		process_stdin(msg);
	}
		
	/* check EOF and HUP */
	if (condition & G_IO_HUP || condition & G_IO_ERR) {
		/* check EOF */
		if (ret == G_IO_STATUS_EOF) {
			print_msg(D_VERBOSE, "EOF from stdin.");
			g_main_loop_quit(main_loop);
			return 0;
		}

	}

	/* bye */
	return 1;
}



