moo/mod/_con.c

294 lines
7.2 KiB
C
Raw Permalink Normal View History

2016-08-20 05:17:47 +00:00
/*
* $Id$
*
Copyright (c) 2014-2017 Chung, Hyung-Hwan. All rights reserved.
2016-08-20 05:17:47 +00:00
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
2017-12-14 02:04:52 +00:00
#include "_con.h"
2017-01-09 09:54:49 +00:00
#include <moo-utl.h>
2016-08-20 05:17:47 +00:00
#include <string.h>
2016-08-20 05:17:47 +00:00
2018-10-14 14:06:58 +00:00
#if defined(_WIN32)
# include <windows.h>
2017-01-09 14:52:15 +00:00
#elif defined(__DOS__)
2016-08-20 05:17:47 +00:00
2017-01-09 14:52:15 +00:00
#else
# include <unistd.h>
# include <fcntl.h>
# include <stdlib.h> /* getenv */
# include <curses.h>
# include <term.h>
# include <sys/ioctl.h>
#endif
2016-08-20 05:17:47 +00:00
typedef struct console_t console_t;
struct console_t
{
int fd;
int fd_opened;
char* cup;
char* clear;
};
/* ------------------------------------------------------------------------ */
static moo_pfrc_t pf_open (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs)
2016-08-20 05:17:47 +00:00
{
#if defined(_WIN32)
HANDLE h;
2017-01-09 09:54:49 +00:00
moo_ooi_t immv;
2016-08-20 05:17:47 +00:00
2019-05-21 14:05:45 +00:00
h = GetStdHandle(STD_OUTPUT_HANDLE);
2016-08-20 05:17:47 +00:00
if (h == INVALID_HANDLE_VALUE) return 0;
if (h == NULL)
{
2019-05-21 14:05:45 +00:00
/* the program doens't have an associated handle for stdin. is it a service? */
2016-08-20 05:17:47 +00:00
}
2019-05-21 14:05:45 +00:00
MOO_STACK_SETRET (moo, nargs, MOO_SMPTR_TO_OOP(h));
2016-08-20 05:17:47 +00:00
2017-01-09 14:52:15 +00:00
#elif defined(__DOS__)
moo->errnum = MOO_ENOIMPL;
return -1;
2016-08-20 05:17:47 +00:00
#else
console_t* con;
int err;
char* term;
con = (console_t*)moo_getobjtrailer(moo, MOO_STACK_GETRCV(moo, nargs), MOO_NULL);
2016-08-20 05:17:47 +00:00
if (isatty(1))
{
con->fd = 1;
}
else
{
2019-05-21 14:05:45 +00:00
con->fd = open("/dev/tty", O_RDWR, 0);
2016-08-20 05:17:47 +00:00
if (con->fd == -1)
{
/* TODO: failed to open /dev/stdout */
2017-01-09 09:54:49 +00:00
moo_freemem (moo, con);
2016-08-20 05:17:47 +00:00
return 0;
}
con->fd_opened = 1;
}
2019-05-21 14:05:45 +00:00
term = getenv("TERM");
if (term && setupterm(term, con->fd, &err) == OK)
2016-08-20 05:17:47 +00:00
{
}
2019-05-21 14:05:45 +00:00
con->cup = tigetstr("cup"); /* TODO: error check */
con->clear = tigetstr("clear");
2016-08-20 05:17:47 +00:00
#if 0
{
const char* cup, * clear;
struct winsize wsz;
cup = tigetstr ("cup");
clear = tigetstr ("clear");
ioctl (fd, TIOCGWINSZ, &wsz);
write (fd, clear, strlen(clear));
/*write (fd, tparm (cup, wsz.ws_row - 1, 0), strlen(tparm (cup, wsz.ws_row - 1, 0)));*/
/*write (fd, tiparm (cup, 0, 0), strlen(tiparm (cup, 0, 0)));*/
write (fd, tparm (cup, 0, 0, 0, 0, 0, 0, 0, 0, 0), strlen(tparm (cup, 0, 0, 0, 0, 0, 0, 0, 0, 0)));
2016-08-20 05:17:47 +00:00
}
#endif
MOO_STACK_SETRET (moo, nargs, MOO_SMOOI_TO_OOP(con->fd));
2017-01-09 14:52:15 +00:00
#endif
2017-01-09 09:54:49 +00:00
return MOO_PF_SUCCESS;
2016-08-20 05:17:47 +00:00
}
static moo_pfrc_t pf_close (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs)
2016-08-20 05:17:47 +00:00
{
#if defined(_WIN32)
2019-05-21 14:05:45 +00:00
moo_oop_t arg;
2016-08-20 05:17:47 +00:00
HANDLE h;
2019-05-21 14:05:45 +00:00
con = MOO_STACK_GETARG(moo, nargs, 0);
h = MOO_OOP_TO_SMPTR(con);
/*if (h != XXX)
{
CloseHandle (h);
}*/
2017-01-09 14:52:15 +00:00
#elif defined(__DOS__)
/* TODO */
2016-08-20 05:17:47 +00:00
#else
console_t* con;
con = (console_t*)moo_getobjtrailer(moo, MOO_STACK_GETRCV(moo, nargs), MOO_NULL);
2016-08-20 05:17:47 +00:00
if (con->fd_opened) close (con->fd);
2017-01-09 09:54:49 +00:00
MOO_STACK_SETRETTORCV (moo, nargs);
return MOO_PF_SUCCESS;
2017-01-09 14:52:15 +00:00
#endif
2016-08-20 05:17:47 +00:00
}
static moo_pfrc_t pf_write (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs)
2016-08-20 05:17:47 +00:00
{
2017-01-09 14:52:15 +00:00
#if defined(_WIN32)
2019-05-21 14:05:45 +00:00
/* TODO: */
2017-01-09 14:52:15 +00:00
#elif defined(__DOS__)
2019-05-21 14:05:45 +00:00
/* TODO: */
2017-01-09 14:52:15 +00:00
#else
2016-08-20 05:17:47 +00:00
console_t* con;
moo_oop_char_t msg;
2016-08-20 05:17:47 +00:00
2017-01-09 09:54:49 +00:00
moo_oow_t ucspos, ucsrem, ucslen, bcslen;
moo_bch_t bcs[1024];
2016-08-20 05:17:47 +00:00
int n;
con = (console_t*)moo_getobjtrailer(moo, MOO_STACK_GETRCV(moo, nargs), MOO_NULL);
msg = (moo_oop_char_t)MOO_STACK_GETARG (moo, nargs, 0);
2016-08-20 05:17:47 +00:00
if (!MOO_OBJ_IS_CHAR_POINTER(msg)) goto einval;
2016-08-20 05:17:47 +00:00
#if defined(MOO_OOCH_IS_UCH)
2016-08-20 05:17:47 +00:00
ucspos = 0;
ucsrem = MOO_OBJ_GET_SIZE(msg);
2016-08-20 05:17:47 +00:00
while (ucsrem > 0)
{
ucslen = ucsrem;
2017-01-09 09:54:49 +00:00
bcslen = MOO_COUNTOF(bcs);
if ((n = moo_convootobchars(moo, &msg->slot[ucspos], &ucslen, bcs, &bcslen)) <= -1)
2016-08-20 05:17:47 +00:00
{
if (n != -2 || ucslen <= 0) return MOO_PF_HARD_FAILURE;
2016-08-20 05:17:47 +00:00
}
write (con->fd, bcs, bcslen); /* TODO: error handling */
/* TODO: abort looping for async processing???? */
ucspos += ucslen;
ucsrem -= ucslen;
}
#else
write (con->fd, MOO_OBJ_GET_CHAR_SLOT(msg), MOO_OBJ_GET_SIZE(msg)); /* TODO: error handling. incomplete write handling */
#endif
2016-08-20 05:17:47 +00:00
2017-01-09 09:54:49 +00:00
MOO_STACK_SETRETTORCV (moo, nargs); /* TODO: change return code */
return MOO_PF_SUCCESS;
einval:
MOO_STACK_SETRETTOERRNUM (moo, nargs); /* TODO: be more specific about the error code */
return MOO_PF_SUCCESS;
2017-01-09 14:52:15 +00:00
#endif
2016-08-20 05:17:47 +00:00
}
static moo_pfrc_t pf_clear (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs)
2016-08-23 00:37:22 +00:00
{
2017-01-09 14:52:15 +00:00
#if defined(_WIN32)
#elif defined(__DOS__)
#else
2016-08-23 00:37:22 +00:00
console_t* con;
con = (console_t*)moo_getobjtrailer(moo, MOO_STACK_GETRCV(moo, nargs), MOO_NULL);
2016-08-23 00:37:22 +00:00
write (con->fd, con->clear, strlen(con->clear));
2017-01-09 09:54:49 +00:00
MOO_STACK_SETRETTORCV (moo, nargs);
return MOO_PF_SUCCESS;
2017-01-09 14:52:15 +00:00
#endif
2016-08-23 00:37:22 +00:00
}
static moo_pfrc_t pf_setcursor (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs)
2016-08-20 05:17:47 +00:00
{
2017-01-09 14:52:15 +00:00
#if defined(_WIN32)
#elif defined(__DOS__)
#else
2016-08-20 05:17:47 +00:00
console_t* con;
moo_oop_t x, y;
2016-08-20 05:17:47 +00:00
char* cup;
con = (console_t*)moo_getobjtrailer(moo, MOO_STACK_GETRCV(moo, nargs), MOO_NULL);
x = MOO_STACK_GETARG(moo, nargs, 0);
y = MOO_STACK_GETARG(moo, nargs, 1);
2016-08-23 00:37:22 +00:00
if (!MOO_OOP_IS_SMOOI(x) || !MOO_OOP_IS_SMOOI(y)) goto einval;
2016-08-20 05:17:47 +00:00
/*cup = tiparm (con->cup, MOO_OOP_TO_SMOOI(y), MOO_OOP_TO_SMOOI(x));*/
cup = tparm (con->cup, MOO_OOP_TO_SMOOI(y), MOO_OOP_TO_SMOOI(x), 0, 0, 0, 0, 0, 0, 0);
2016-08-20 05:17:47 +00:00
write (con->fd, cup, strlen(cup)); /* TODO: error check */
2016-08-23 00:37:22 +00:00
2017-01-09 09:54:49 +00:00
MOO_STACK_SETRETTORCV (moo, nargs);
return MOO_PF_SUCCESS;
einval:
MOO_STACK_SETRETTOERRNUM (moo, nargs); /* TODO: be more specific about the error code */
return MOO_PF_SUCCESS;
2017-01-09 14:52:15 +00:00
#endif
2016-08-20 05:17:47 +00:00
}
/* ------------------------------------------------------------------------ */
#define C MOO_METHOD_CLASS
#define I MOO_METHOD_INSTANCE
2016-08-20 05:17:47 +00:00
static moo_pfinfo_t pfinfos[] =
2016-08-20 05:17:47 +00:00
{
{ I, "clear", { pf_clear, 0, 0 } },
{ I, "close", { pf_close, 0, 0 } },
{ I, "open", { pf_open, 0, 0 } },
{ I, "setcursor", { pf_setcursor, 2, 2 } },
{ I, "write", { pf_write, 1, 1 } }
2016-08-20 05:17:47 +00:00
};
/* ------------------------------------------------------------------------ */
static int import (moo_t* moo, moo_mod_t* mod, moo_oop_class_t _class)
{
if (moo_setclasstrsize(moo, _class, MOO_SIZEOF(console_t), MOO_NULL) <= -1) return -1;
return 0;
}
static moo_pfbase_t* querypf (moo_t* moo, moo_mod_t* mod, const moo_ooch_t* name, moo_oow_t namelen)
2016-08-20 05:17:47 +00:00
{
return moo_findpfbase(moo, pfinfos, MOO_COUNTOF(pfinfos), name, namelen);
2016-08-20 05:17:47 +00:00
}
2017-01-09 09:54:49 +00:00
static void unload (moo_t* moo, moo_mod_t* mod)
2016-08-20 05:17:47 +00:00
{
/* TODO: close all open handle?? */
}
2017-12-14 02:04:52 +00:00
int moo_mod_con (moo_t* moo, moo_mod_t* mod)
2016-08-20 05:17:47 +00:00
{
mod->import = import;
mod->querypf = querypf;
mod->querypv = MOO_NULL;
2016-10-01 01:56:14 +00:00
mod->unload = unload;
2017-01-09 09:54:49 +00:00
mod->ctx = MOO_NULL;
2016-08-20 05:17:47 +00:00
return 0;
}