Index: asp3_tinet_ecnl_arm/trunk/ntshell/src/fdtable.c
===================================================================
--- asp3_tinet_ecnl_arm/trunk/ntshell/src/fdtable.c	(revision 374)
+++ asp3_tinet_ecnl_arm/trunk/ntshell/src/fdtable.c	(revision 387)
@@ -44,26 +44,5 @@
 #include "syssvc/syslog.h"
 #include "target_syssvc.h"
-#ifndef NTSHELL_NO_SOCKET
-#include <tinet_defs.h>
-#include <tinet_config.h>
-#include <net/net.h>
-#include <net/net_endian.h>
-#include <netinet/in.h>
-#include <netinet/in_itron.h>
-#include <tinet_nic_defs.h>
-#include <tinet_cfg.h>
-#include <netinet/in_var.h>
-#include <net/ethernet.h>
-#include <net/if6_var.h>
-#include <net/net.h>
-#include <net/if_var.h>
-#include <netinet/udp.h>
-#include <netinet/udp_var.h>
-#include <netinet/tcp.h>
-#include <netinet/tcp_var.h>
-#include <net/net_buf.h>
-#endif
-#include "ff.h"
-#include "socket_stub.h"
+#include "fdtable.h"
 #include "kernel_cfg.h"
 #include <string.h>
@@ -75,147 +54,14 @@
 #endif
 
-static int stdio_close(struct SHELL_FILE *fp);
-static size_t stdio_read(struct SHELL_FILE *fp, unsigned char *data, size_t len);
-static size_t stdio_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len);
-static size_t stdin_read(struct SHELL_FILE *fp, unsigned char *data, size_t len);
-static size_t stdout_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len);
-static size_t stderr_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len);
-static void stdio_delete(struct SHELL_FILE *fp);
-
-static int sio_close(struct SHELL_FILE *fp);
-static size_t sio_read(struct SHELL_FILE *fp, unsigned char *data, size_t len);
-static size_t sio_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len);
-static off_t sio_seek(struct SHELL_FILE *fp, off_t ofs, int org);
-static int sio_ioctl(struct SHELL_FILE *fp, int req, void *arg);
-static bool_t sio_readable(struct SHELL_FILE *fp);
-static void sio_delete(struct SHELL_FILE *fp);
-
-IO_TYPE IO_TYPE_STDIN = { stdio_close, stdin_read, stdio_write, sio_seek, sio_ioctl, sio_readable, stdio_delete };
-IO_TYPE IO_TYPE_STDOUT = { stdio_close, stdio_read, stdout_write, sio_seek, sio_ioctl, sio_readable, stdio_delete };
-IO_TYPE IO_TYPE_STDERR = { stdio_close, stdio_read, stderr_write, sio_seek, sio_ioctl, sio_readable, stdio_delete };
-IO_TYPE IO_TYPE_SIO = { sio_close, sio_read, sio_write, sio_seek, sio_ioctl, sio_readable, sio_delete };
-ntstdio_t ntstdio;
+extern IO_TYPE IO_TYPE_STDIN;
+extern IO_TYPE IO_TYPE_STDOUT;
+extern IO_TYPE IO_TYPE_STDERR;
 
 static struct SHELL_FILE fd_table[8 * sizeof(FLGPTN)] = {
-	{ 0, &IO_TYPE_STDIN, 0, .exinf = &ntstdio },
-	{ 1, &IO_TYPE_STDOUT, 0, .exinf = &ntstdio },
-	{ 2, &IO_TYPE_STDERR, 0,.exinf = &ntstdio },
+	{ STDIN_FILENO, &IO_TYPE_STDIN, 0 },
+	{ STDOUT_FILENO, &IO_TYPE_STDOUT, 0 },
+	{ STDERR_FILENO, &IO_TYPE_STDERR, 0 },
 };
 #define fd_table_count (sizeof(fd_table) / sizeof(fd_table[0]))
-
-extern ntstdio_t ntstdio;
-extern serial_t stdio_uart;
-
-unsigned char ntstdio_xi(struct ntstdio_t *handle)
-{
-	return serial_getc((serial_t *)handle->exinf);
-}
-
-void ntstdio_xo(struct ntstdio_t *handle, unsigned char c)
-{
-	serial_putc((serial_t *)handle->exinf, c);
-}
-
-void sys_init(intptr_t exinf)
-{
-	ntstdio_init(&ntstdio, NTSTDIO_OPTION_LINE_ECHO | NTSTDIO_OPTION_CANON | NTSTDIO_OPTION_LF_CRLF | NTSTDIO_OPTION_LF_CR, ntstdio_xi, ntstdio_xo);
-	ntstdio.exinf = (void *)&stdio_uart;
-}
-
-int stdio_close(struct SHELL_FILE *fp)
-{
-	return -EPERM;
-}
-
-size_t stdio_read(struct SHELL_FILE *fp, unsigned char *data, size_t len)
-{
-	return -EPERM;
-}
-
-size_t stdio_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len)
-{
-	return -EPERM;
-}
-
-size_t stdin_read(struct SHELL_FILE *fp, unsigned char *data, size_t len)
-{
-	int i = 0;
-	while (i < len) {
-		int c = ntstdio_getc((struct ntstdio_t *)fp->exinf);
-		data[i++] = c;
-		if ((c == EOF) || (c == '\n'))
-			break;
-	}
-	return i;
-}
-
-size_t stdout_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len)
-{
-	for (int i = 0; i < len; i++) {
-		ntstdio_putc((struct ntstdio_t *)fp->exinf, data[i]);
-	}
-	return len;
-}
-
-size_t stderr_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len)
-{
-	for (int i = 0; i < len; i++) {
-		ntstdio_putc((struct ntstdio_t *)fp->exinf, data[i]);
-	}
-	return len;
-}
-
-void stdio_delete(struct SHELL_FILE *fp)
-{
-}
-
-int sio_close(struct SHELL_FILE *fp)
-{
-	return -EPERM;
-}
-
-size_t sio_read(struct SHELL_FILE *fp, unsigned char *data, size_t len)
-{
-	return -EPERM;
-}
-
-size_t sio_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len)
-{
-	return -EPERM;
-}
-
-off_t sio_seek(struct SHELL_FILE *fp, off_t ofs, int org)
-{
-	return -EPERM;
-}
-
-int sio_ioctl(struct SHELL_FILE *fp, int request, void *arg)
-{
-	switch (request) {
-	case TIOCGWINSZ:
-		return 0;
-	case TCGETS:
-		return sio_tcgetattr(fp->fd, (struct termios *)arg);
-	case TCSETS + TCSANOW:
-	case TCSETS + TCSADRAIN:
-	case TCSETS + TCSAFLUSH:
-		return sio_tcsetattr(fp->fd, request - TCSETS, (const struct termios *)arg);
-	}
-
-	return -EINVAL;
-}
-
-bool_t sio_readable(struct SHELL_FILE *fp)
-{
-	return fp->readevt_w != fp->readevt_r;
-}
-
-void sio_delete(struct SHELL_FILE *fp)
-{
-	free((serial_t *)((struct ntstdio_t *)fp->exinf)->exinf);
-	((struct ntstdio_t *)fp->exinf)->exinf = NULL;
-	free((struct ntstdio_t *)fp->exinf);
-	fp->exinf = NULL;
-}
 
 struct SHELL_FILE *new_fp(IO_TYPE *type, int id, int writable)
@@ -245,4 +91,15 @@
 	if (ret < 0) {
 		syslog(LOG_ERROR, "sig_sem => %d", ret);
+	}
+
+	if (fp != NULL) {
+		FLGPTN flgptn = 0;
+
+		FD_SET(fp->fd, (fd_set *)&flgptn);
+
+		ret = clr_flg(FLG_SELECT_WAIT, ~flgptn);
+		if (ret != E_OK) {
+			syslog(LOG_ERROR, "clr_flg => %d", ret);
+		}
 	}
 
@@ -440,50 +297,9 @@
 }
 
-/* TODO:コールバック化したい */
-void stdio_update_evts()
-{
-	int fd = STDIN_FILENO;
-	struct SHELL_FILE *fp = &fd_table[fd];
-	FLGPTN flgptn = 0;
-
-	if (serial_readable((serial_t *)((struct ntstdio_t *)fp->exinf)->exinf)) {
-		if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;
-
-		FD_SET(fd, (fd_set *)&flgptn);
-	}
-	if (serial_writable((serial_t *)((struct ntstdio_t *)fp->exinf)->exinf)) {
-		if (fp->writeevt_w == fp->writeevt_r) fp->writeevt_w++;
-
-		FD_SET(fd, (fd_set *)&flgptn);
-	}
-
-	if (flgptn != 0) {
-		set_flg(FLG_SELECT_WAIT, flgptn);
-	}
-}
-
-/* TODO:コールバック化したい */
-void stdio_flgptn(FLGPTN *flgptn)
-{
-	int fd = STDIN_FILENO;
-	struct SHELL_FILE *fp = &fd_table[fd];
-	*flgptn = 0;
-
-	if (serial_readable((serial_t *)((struct ntstdio_t *)fp->exinf)->exinf)) {
-		if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;
-
-		FD_SET(fd, (fd_set *)flgptn);
-	}
-	if (serial_writable((serial_t *)((struct ntstdio_t *)fp->exinf)->exinf)) {
-		if (fp->writeevt_w == fp->writeevt_r) fp->writeevt_w++;
-
-		FD_SET(fd, (fd_set *)flgptn);
-	}
-}
-
 ER shell_get_evts(struct fd_events *evts, TMO tmout)
 {
 	int count = 0;
 	SYSTIM prev, now;
+	FLGPTN flgptn;
 
 	get_tim(&prev);
@@ -491,8 +307,6 @@
 	for (;;) {
 		ER ret;
-		FLGPTN waitptn, flgptn, readfds = 0, writefds = 0;
+		FLGPTN waitptn, readfds = 0, writefds = 0;
 		struct SHELL_FILE *fp = NULL;
-
-		stdio_update_evts();
 
 #ifndef NTSHELL_NO_SOCKET
@@ -504,5 +318,4 @@
 			fp = &fd_table[fd];
 
-#ifndef NTSHELL_NO_SOCKET
 			if (FD_ISSET(fd, &evts->readfds)) {
 				if (fp->type->readable(fp)) {
@@ -515,7 +328,7 @@
 				}
 			}
-#endif
+
 			if (FD_ISSET(fd, &evts->writefds)) {
-				if (fp->writeevt_w == fp->writeevt_r) {
+				if (fp->type->writable(fp)) {
 					FD_SET(fd, (fd_set *)&writefds);
 					count++;
@@ -547,46 +360,41 @@
 				return ret;
 			}
-
-			stdio_flgptn(&flgptn);
-
-			if (flgptn == 0)
-				return E_TMOUT;
-		}
-		flgptn &= waitptn;
-
-		/* 受け取ったフラグのみクリア */
-		ret = clr_flg(FLG_SELECT_WAIT, ~flgptn);
-		if (ret != E_OK) {
-			syslog(LOG_ERROR, "clr_flg => %d", ret);
+		}
+
+		if (flgptn != 0) {
+			flgptn &= waitptn;
+
+			/* 受け取ったフラグのみクリア */
+			ret = clr_flg(FLG_SELECT_WAIT, ~flgptn);
+			if (ret != E_OK) {
+				syslog(LOG_ERROR, "clr_flg => %d", ret);
+			}
 		}
 
 		count = 0;
 		for (int fd = 0; fd < fd_table_count; fd++) {
-			if (!FD_ISSET(fd, (fd_set *)&waitptn))
-				continue;
-
 			fp = &fd_table[fd];
 
 			if (fp->readevt_w != fp->readevt_r) {
 				fp->readevt_r++;
-				FD_SET(fd, &evts->readfds);
+				if (FD_ISSET(fd, (fd_set *)&waitptn))
+					FD_SET(fd, &evts->readfds);
 				count++;
 			}
 			if (fp->writeevt_w != fp->writeevt_r) {
 				fp->writeevt_r++;
-				fp->writable = 1;
-			}
-			if (fp->writable) {
-				FD_SET(fd, &evts->writefds);
+				if (FD_ISSET(fd, (fd_set *)&waitptn))
+					FD_SET(fd, &evts->writefds);
 				count++;
 			}
 			if (fp->errorevt_w != fp->errorevt_r) {
 				fp->errorevt_r++;
-				FD_SET(fd, &evts->errorfds);
+				if (FD_ISSET(fd, (fd_set *)&waitptn))
+					FD_SET(fd, &evts->errorfds);
 				count++;
 			}
 		}
 
-		if (count > 0)
+		if ((flgptn == 0) || (count > 0))
 			break;
 
@@ -594,6 +402,8 @@
 
 		SYSTIM elapse = now - prev;
-		if (elapse > tmout)
-			return E_TMOUT;
+		if (elapse > tmout) {
+			flgptn = 0;
+			break;
+		}
 
 		prev = now;
@@ -603,5 +413,5 @@
 	evts->count = count;
 
-	return E_OK;
+	return (flgptn == 0) ? E_TMOUT : E_OK;
 }
 
