| 1 |
/* |
| 2 |
* Copyright (c) 2003-2005 RIKEN Japan, All rights reserved. |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions |
| 6 |
* are met: |
| 7 |
* |
| 8 |
* 1. Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* |
| 11 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 12 |
* notice, this list of conditions and the following disclaimer in the |
| 13 |
* documentation and/or other materials provided with the distribution. |
| 14 |
* |
| 15 |
* THIS SOFTWARE IS PROVIDED BY RIKEN AND CONTRIBUTORS ``AS IS'' AND ANY |
| 16 |
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 18 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RIKEN OR CONTRIBUTORS BE |
| 19 |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 25 |
* THE POSSIBILITY OF SUCH DAMAGE. |
| 26 |
*/ |
| 27 |
|
| 28 |
/* $Id: termios_console.cpp,v 1.3 2005/03/01 14:10:54 orrisroot Exp $ */ |
| 29 |
|
| 30 |
#ifdef HAVE_CONFIG_H |
| 31 |
# include "config.h" |
| 32 |
#endif |
| 33 |
#include <stdio.h> |
| 34 |
#include <stdlib.h> |
| 35 |
#include <string.h> |
| 36 |
#include <signal.h> |
| 37 |
#include <setjmp.h> |
| 38 |
|
| 39 |
#include "libsl4.h" |
| 40 |
#include "libsatellite.h" |
| 41 |
|
| 42 |
#include "SL_header.h" |
| 43 |
#include "SL_exception.h" |
| 44 |
#include "history.h" |
| 45 |
#include "module.h" |
| 46 |
#include "tty_console.h" |
| 47 |
#include "termios_console.h" |
| 48 |
|
| 49 |
#include "terminal.h" |
| 50 |
|
| 51 |
#ifdef RETSIGTYPE |
| 52 |
typedef RETSIGTYPE (*sigfunc_t)(int); |
| 53 |
#else |
| 54 |
typedef void (*sigfunc_t)(int); |
| 55 |
#endif |
| 56 |
|
| 57 |
termios_console::termios_console() { |
| 58 |
ifd.fd = fileno(stdin); |
| 59 |
ofd.fd = fileno(stdout); |
| 60 |
efd.fd = fileno(stderr); |
| 61 |
sl4_term_init(stdin, stdout, stderr); |
| 62 |
sl4_term_clear_screen(); |
| 63 |
} |
| 64 |
|
| 65 |
bool termios_console::term_has_color(){ |
| 66 |
return false; // TODO termcap |
| 67 |
} |
| 68 |
|
| 69 |
int termios_console::term_keypad_getc(){ |
| 70 |
int ret; |
| 71 |
ret = sl4_term_keypad_getc(); |
| 72 |
switch(ret){ |
| 73 |
case SL4_KEYPAD_UP: ret = SL_TTY::UPKEY << 8; break; |
| 74 |
case SL4_KEYPAD_DOWN: ret = SL_TTY::DOWNKEY << 8; break; |
| 75 |
case SL4_KEYPAD_LEFT: ret = SL_TTY::LEFTKEY << 8; break; |
| 76 |
case SL4_KEYPAD_RIGHT: ret = SL_TTY::RIGHTKEY << 8; break; |
| 77 |
case SL4_KEYPAD_HOME: ret = SL_TTY::HOMEKEY << 8; break; |
| 78 |
case SL4_KEYPAD_END: ret = SL_TTY::ENDKEY << 8; break; |
| 79 |
} |
| 80 |
return ret; |
| 81 |
} |
| 82 |
|
| 83 |
static sigjmp_buf jmpenv; |
| 84 |
void sigintjmp(int sig){ |
| 85 |
signal(SIGINT, SIG_IGN); |
| 86 |
siglongjmp(jmpenv, 1); |
| 87 |
} |
| 88 |
|
| 89 |
char *termios_console::term_gets(char *buf, int size){ |
| 90 |
char *ret; |
| 91 |
sigfunc_t sigoldfunc; |
| 92 |
sigoldfunc = signal(SIGINT, SIG_IGN); |
| 93 |
if(sigsetjmp(jmpenv, 1) == 0){ |
| 94 |
signal(SIGINT, sigintjmp); |
| 95 |
ret = sl4_term_gets(buf, size); |
| 96 |
}else{ |
| 97 |
sl4_term_putc('\n'); |
| 98 |
ret = NULL; /* sig int caught */ |
| 99 |
} |
| 100 |
signal(SIGINT, sigoldfunc); |
| 101 |
return ret; |
| 102 |
} |
| 103 |
|
| 104 |
void termios_console::term_flush(sl4_fd_t fd){ |
| 105 |
FILE *fp; |
| 106 |
switch(fd.fd){ |
| 107 |
case -1: break; |
| 108 |
case 0: /* fflush(stdin); */ break; |
| 109 |
case 1: fflush(stdout); break; |
| 110 |
case 2: fflush(stderr); break; |
| 111 |
default: |
| 112 |
fp = fdopen(fd.fd,"w"); |
| 113 |
if(fp != NULL){ |
| 114 |
fflush(fp); |
| 115 |
fclose(fp); |
| 116 |
} |
| 117 |
} |
| 118 |
} |