| 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: terminal.c,v 1.4 2006/09/01 02:43:10 orrisroot Exp $ */ |
| 29 |
|
| 30 |
#ifdef HAVE_CONFIG_H |
| 31 |
# include "config.h" |
| 32 |
#endif |
| 33 |
|
| 34 |
#include <stdio.h> |
| 35 |
#include <stdlib.h> |
| 36 |
#include <string.h> |
| 37 |
#include <signal.h> |
| 38 |
|
| 39 |
#ifndef WIN32 |
| 40 |
|
| 41 |
#if defined(HAVE_CURSES_H) && defined(HAVE_TERM_H) |
| 42 |
# include <curses.h> |
| 43 |
# include <term.h> |
| 44 |
#else |
| 45 |
# ifdef HAVE_TERMCAP_H |
| 46 |
# include <termcap.h> |
| 47 |
# endif |
| 48 |
#endif |
| 49 |
#ifdef HAVE_SYS_IOCTL_H |
| 50 |
#include <sys/ioctl.h> |
| 51 |
#endif |
| 52 |
#ifdef HAVE_SYS_TYPES_H |
| 53 |
# include <sys/types.h> |
| 54 |
#endif |
| 55 |
|
| 56 |
#include "terminal.h" |
| 57 |
|
| 58 |
#include <libsl4.h> |
| 59 |
|
| 60 |
#define TC_ENV_SIZE 2048 |
| 61 |
#define TC_STR_IS_SET(x) (term.tcstr[(x)] != NULL) |
| 62 |
#define TC_KEY_IS_SET(x) (term.tckey[(x)] != NULL) |
| 63 |
#define TC_STR(x) term.tcstr[(x)] |
| 64 |
#define TC_KEY(x) term.tckey[(x)] |
| 65 |
|
| 66 |
/* |
| 67 |
#define TC_co 0 |
| 68 |
#define TC_li 1 |
| 69 |
*/ |
| 70 |
|
| 71 |
#ifdef __cplusplus |
| 72 |
extern "C" { |
| 73 |
#endif |
| 74 |
|
| 75 |
static struct term_keypad_t { |
| 76 |
char *key; |
| 77 |
int code; |
| 78 |
} term_keypad[] = { |
| 79 |
#define KEYPAD_UP 0 |
| 80 |
{ "\x1b[A", SL4_KEYPAD_UP }, |
| 81 |
#define KEYPAD_DOWN 1 |
| 82 |
{ "\x1b[B", SL4_KEYPAD_DOWN }, |
| 83 |
#define KEYPAD_RIGHT 2 |
| 84 |
{ "\x1b[C", SL4_KEYPAD_RIGHT }, |
| 85 |
#define KEYPAD_LEFT 3 |
| 86 |
{ "\x1b[D", SL4_KEYPAD_LEFT }, |
| 87 |
#define KEYPAD_HOME 4 |
| 88 |
{ "\x1b[H", SL4_KEYPAD_HOME }, |
| 89 |
#define KEYPAD_END 5 |
| 90 |
{ "\x1b[F", SL4_KEYPAD_END }, |
| 91 |
#define KEYPAD_UP2 6 |
| 92 |
{ "\x1bOA", SL4_KEYPAD_UP }, |
| 93 |
#define KEYPAD_DOWN2 7 |
| 94 |
{ "\x1bOB", SL4_KEYPAD_DOWN }, |
| 95 |
#define KEYPAD_RIGHT2 8 |
| 96 |
{ "\x1bOC", SL4_KEYPAD_RIGHT }, |
| 97 |
#define KEYPAD_LEFT2 9 |
| 98 |
{ "\x1bOD", SL4_KEYPAD_LEFT }, |
| 99 |
#define KEYPAD_HOME2 10 |
| 100 |
{ "\x1bOH", SL4_KEYPAD_HOME }, |
| 101 |
#define KEYPAD_END2 11 |
| 102 |
{ "\x1bOF", SL4_KEYPAD_END }, |
| 103 |
#define KEYPAD_DELETE 12 |
| 104 |
{ "\x7f", 0x08 } |
| 105 |
#define KEYPAD_SIZE 13 |
| 106 |
}; |
| 107 |
|
| 108 |
static char *term_val_names[]={ |
| 109 |
#define TC_km 0 |
| 110 |
"km", /* Has Meta Key */ |
| 111 |
#define TC_MT 1 |
| 112 |
"MT", /* Has Meta Key ? */ |
| 113 |
#define TC_VAL_SIZE 2 |
| 114 |
NULL |
| 115 |
}; |
| 116 |
|
| 117 |
static char *term_key_names[]={ |
| 118 |
#define TC_kd 0 |
| 119 |
"kd", /* sends cursor down */ |
| 120 |
#define TC_kl 1 |
| 121 |
"kl", /* sends cursor left */ |
| 122 |
#define TC_kr 2 |
| 123 |
"kr", /* sends cursor right */ |
| 124 |
#define TC_ku 3 |
| 125 |
"ku", /* sends cursor up */ |
| 126 |
#define TC_kh 4 |
| 127 |
"kh", /* sends cursor home */ |
| 128 |
#define TC_at7 5 |
| 129 |
"@7", /* sends cursor end */ |
| 130 |
#define TC_kb 6 |
| 131 |
"kb", /* backspace key */ |
| 132 |
#define TC_kD 7 |
| 133 |
"kD", /* delete-character key */ |
| 134 |
#define TC_KEY_SIZE 8 |
| 135 |
NULL |
| 136 |
}; |
| 137 |
|
| 138 |
static char *term_str_names[]={ |
| 139 |
#define TC_cr 0 |
| 140 |
"cr", /* carrige return */ |
| 141 |
#define TC_cl 1 |
| 142 |
"cl", /* clear screen */ |
| 143 |
#define TC_ho 2 |
| 144 |
"ho", /* home cursor */ |
| 145 |
#define TC_cd 3 |
| 146 |
"cd", /* clear to bottom */ |
| 147 |
#define TC_ce 4 |
| 148 |
"ce", /* clear to end of line */ |
| 149 |
#define TC_nw 5 |
| 150 |
"nw", /* new line */ |
| 151 |
#define TC_mb 6 |
| 152 |
"mb", /* begin blink mode */ |
| 153 |
#define TC_md 7 |
| 154 |
"md", /* begin bold mode */ |
| 155 |
#define TC_se 8 |
| 156 |
"se", /* end standout mode */ |
| 157 |
#define TC_so 9 |
| 158 |
"so", /* begin standout mode */ |
| 159 |
#define TC_us 10 |
| 160 |
"us", /* begin underline mode */ |
| 161 |
#define TC_ue 11 |
| 162 |
"ue", /* end underline mode */ |
| 163 |
#define TC_me 12 |
| 164 |
"me", /* end attributes */ |
| 165 |
#define TC_bl 13 |
| 166 |
"bl", /* audible bell */ |
| 167 |
#define TC_vb 14 |
| 168 |
"vb", /* visible bell */ |
| 169 |
#define TC_do 15 |
| 170 |
"do", /* cursor down one */ |
| 171 |
#define TC_le 16 |
| 172 |
"le", /* cursor left one */ |
| 173 |
#define TC_nd 17 |
| 174 |
"nd", /* cursor right one (non destructive space) */ |
| 175 |
#define TC_up 18 |
| 176 |
"up", /* cursor up one */ |
| 177 |
#define TC_ip 19 |
| 178 |
"ip", /* insert padding */ |
| 179 |
#define TC_dm 20 |
| 180 |
"dm", /* begin delete mode */ |
| 181 |
#define TC_ed 21 |
| 182 |
"ed", /* end delete mode */ |
| 183 |
#define TC_im 22 |
| 184 |
"im", /* begin insert mode */ |
| 185 |
#define TC_ei 23 |
| 186 |
"ei", /* end insert mode */ |
| 187 |
#define TC_dc 24 |
| 188 |
"dc", /* delete a character */ |
| 189 |
#define TC_ic 25 |
| 190 |
"ic", /* insert a character */ |
| 191 |
#define TC_DC 26 |
| 192 |
"DC", /* delete multiple chars */ |
| 193 |
#define TC_IC 27 |
| 194 |
"IC", /* insert multiple chars */ |
| 195 |
#define TC_DO 28 |
| 196 |
"DO", /* cursor down multiple */ |
| 197 |
#define TC_LE 29 |
| 198 |
"LE", /* cursor left multiple */ |
| 199 |
#define TC_RI 30 |
| 200 |
"RI", /* cursor right multiple */ |
| 201 |
#define TC_UP 31 |
| 202 |
"UP", /* cursor up multiple */ |
| 203 |
#define TC_ve 32 |
| 204 |
"ve", /* make cursor appear normal */ |
| 205 |
#define TC_vi 33 |
| 206 |
"vi", /* make cursor invisible */ |
| 207 |
#define TC_STR_SIZE 34 |
| 208 |
NULL |
| 209 |
}; |
| 210 |
|
| 211 |
/* "co", */ /* number of columns */ |
| 212 |
/* "li", */ /* number of lines */ |
| 213 |
|
| 214 |
typedef struct _sl4_term_t { |
| 215 |
int ttyin, ttyout, ttyerr; |
| 216 |
FILE *fin, *fout, *ferr; |
| 217 |
/* tty information */ |
| 218 |
struct termios tty_normal, tty_raw, tty_cook, tty_quote; |
| 219 |
char tty_mode; /* TTY_MODE */ |
| 220 |
/* terminal capability information */ |
| 221 |
char *env; |
| 222 |
char tcap[TC_ENV_SIZE]; |
| 223 |
char **tcstr; |
| 224 |
char **tckey; |
| 225 |
int *tcval; |
| 226 |
/* terminal information */ |
| 227 |
int col, lin; |
| 228 |
/* sigwinch */ |
| 229 |
int got_sigwinch; |
| 230 |
} sl4_term_t; |
| 231 |
|
| 232 |
static sl4_term_t term; |
| 233 |
static void sigwinch_handler(int signo); |
| 234 |
static int _sl4_tty_init(); |
| 235 |
static int _sl4_tty_quit(); |
| 236 |
static int _sl4_term_refresh_wsize(); |
| 237 |
static int _sl4_tcstr_set(int n, const char *str); |
| 238 |
static int _sl4_tckey_set(int n, const char *str); |
| 239 |
|
| 240 |
static void sigwinch_handler(int signo){ |
| 241 |
term.got_sigwinch = 1; |
| 242 |
} |
| 243 |
|
| 244 |
static int _sl4_tty_init(){ |
| 245 |
/* get current terminal capability attributes */ |
| 246 |
tcgetattr(term.ttyin, &term.tty_normal); |
| 247 |
term.tty_raw = term.tty_normal; |
| 248 |
/* make raw mode - cfmakeraw(&tty_raw); */ |
| 249 |
term.tty_raw.c_iflag &= ~(BRKINT|ISTRIP|INLCR|IGNCR|ICRNL|IXON|IXOFF); |
| 250 |
term.tty_raw.c_oflag &= ~OPOST; |
| 251 |
term.tty_raw.c_lflag &= ~(ICANON|IEXTEN|ECHO|ECHOE|ECHOK|ECHONL|ISIG); |
| 252 |
term.tty_raw.c_cc[VMIN] = 1; |
| 253 |
term.tty_raw.c_cc[VTIME] = 0; |
| 254 |
/* make cooked mode */ |
| 255 |
/* TODO */ |
| 256 |
term.tty_cook = term.tty_normal; |
| 257 |
/* make quoted mode */ |
| 258 |
/* TODO */ |
| 259 |
term.tty_quote = term.tty_raw; |
| 260 |
/* set now mode */ |
| 261 |
term.tty_mode = TTY_MODE_NORMAL; |
| 262 |
return 0; |
| 263 |
} |
| 264 |
|
| 265 |
static int _sl4_tty_quit(){ |
| 266 |
sl4_tty_setmode(TTY_MODE_NORMAL); |
| 267 |
return 0; |
| 268 |
} |
| 269 |
|
| 270 |
static int _sl4_term_refresh_wsize(){ |
| 271 |
#ifdef TIOCGWINSZ |
| 272 |
struct winsize ws; |
| 273 |
#endif /* TIOCGWINSZ */ |
| 274 |
#ifdef TIOCGSIZE |
| 275 |
struct ttysize ts; |
| 276 |
#endif /* TIOCGSIZE */ |
| 277 |
#ifdef TIOCGWINSZ |
| 278 |
if (ioctl(0, TIOCGWINSZ, &ws) != -1) { |
| 279 |
if(ws.ws_col) term.col = ws.ws_col; |
| 280 |
if(ws.ws_row) term.lin = ws.ws_row; |
| 281 |
} |
| 282 |
#endif /* TIOCGWINSZ */ |
| 283 |
#ifdef TIOCGSIZE |
| 284 |
if(ioctl(0, TIOCGSIZE, &ts) != -1) { |
| 285 |
if(ts.ts_cols) term.col = ts.ts_cols; |
| 286 |
if(ts.ts_lines) term.lin = ts.ts_lines; |
| 287 |
} |
| 288 |
#endif /* TIOCGSIZE */ |
| 289 |
return 0; |
| 290 |
} |
| 291 |
|
| 292 |
static int _sl4_tcstr_set(int n, const char *str){ |
| 293 |
if(term.tcstr[n] != NULL){ |
| 294 |
free(term.tcstr[n]); |
| 295 |
term.tcstr[n] = NULL; |
| 296 |
} |
| 297 |
if(str != NULL && *str != '\0'){ |
| 298 |
term.tcstr[n] = (char*)malloc(sizeof(char)*(strlen(str)+1)); |
| 299 |
if(term.tcstr[n] == NULL) |
| 300 |
return -1; |
| 301 |
strcpy(term.tcstr[n], str); /* safe */ |
| 302 |
} |
| 303 |
return 0; |
| 304 |
} |
| 305 |
|
| 306 |
static int _sl4_tckey_set(int n, const char *str){ |
| 307 |
if(term.tckey[n] != NULL){ |
| 308 |
free(term.tckey[n]); |
| 309 |
term.tckey[n] = NULL; |
| 310 |
} |
| 311 |
if(str != NULL && *str != '\0'){ |
| 312 |
term.tckey[n] = (char*)malloc(sizeof(char)*(strlen(str)+1)); |
| 313 |
if(term.tckey[n] == NULL) |
| 314 |
return -1; |
| 315 |
strcpy(term.tckey[n], str); /* safe */ |
| 316 |
} |
| 317 |
return 0; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
int sl4_tty_setmode(int mode){ |
| 322 |
int ret = 0; |
| 323 |
switch(mode){ |
| 324 |
case TTY_MODE_NORMAL: |
| 325 |
if(term.tty_mode != TTY_MODE_NORMAL){ |
| 326 |
tcsetattr(0,TCSADRAIN,&term.tty_normal); |
| 327 |
term.tty_mode = TTY_MODE_NORMAL; |
| 328 |
} |
| 329 |
break; |
| 330 |
case TTY_MODE_RAW: |
| 331 |
if(term.tty_mode != TTY_MODE_RAW){ |
| 332 |
tcsetattr(0,TCSADRAIN,&term.tty_raw); |
| 333 |
term.tty_mode = TTY_MODE_RAW; |
| 334 |
} |
| 335 |
break; |
| 336 |
case TTY_MODE_COOK: |
| 337 |
if(term.tty_mode != TTY_MODE_COOK){ |
| 338 |
tcsetattr(0,TCSADRAIN,&term.tty_cook); |
| 339 |
term.tty_mode = TTY_MODE_COOK; |
| 340 |
} |
| 341 |
break; |
| 342 |
default: |
| 343 |
ret = -1; |
| 344 |
} |
| 345 |
return ret; |
| 346 |
} |
| 347 |
|
| 348 |
int sl4_term_init(FILE *fin, FILE *fout, FILE *ferr){ |
| 349 |
/* term */ |
| 350 |
int i,ent; |
| 351 |
char *area,*tmp; |
| 352 |
char buf[TC_ENV_SIZE]; |
| 353 |
sigset_t oset, nset; |
| 354 |
struct sigaction sigact; |
| 355 |
|
| 356 |
/* set file pointer */ |
| 357 |
term.fin = fin; |
| 358 |
term.fout = fout; |
| 359 |
term.ferr = ferr; |
| 360 |
/* set file discripter */ |
| 361 |
term.ttyin = fileno(fin); |
| 362 |
term.ttyout = fileno(fout); |
| 363 |
term.ttyerr = fileno(ferr); |
| 364 |
|
| 365 |
/* initialize tty */ |
| 366 |
_sl4_tty_init(); |
| 367 |
|
| 368 |
/* block SIGWINCH signal */ |
| 369 |
sigemptyset(&nset); |
| 370 |
sigaddset(&nset, SIGWINCH); |
| 371 |
sigprocmask(SIG_BLOCK, &nset, &oset); |
| 372 |
|
| 373 |
area = buf; |
| 374 |
tmp = sl4_getenv("TERM"); |
| 375 |
if(tmp == NULL || *tmp == '\0') |
| 376 |
tmp = "dumb"; |
| 377 |
term.env = (char*)malloc(sizeof(char)*(strlen(tmp)+1)); |
| 378 |
if(term.env){ |
| 379 |
strcpy(term.env, tmp); /* safe */ |
| 380 |
} |
| 381 |
|
| 382 |
memset(term.tcap, 0, TC_ENV_SIZE); |
| 383 |
term.tcstr = (char**)malloc(sizeof(char*)*TC_STR_SIZE); |
| 384 |
if(term.tcstr) |
| 385 |
for(i=0;i<TC_STR_SIZE;i++) term.tcstr[i] = NULL; |
| 386 |
term.tckey = (char**)malloc(sizeof(char*)*TC_KEY_SIZE); |
| 387 |
if(term.tckey) |
| 388 |
for(i=0;i<TC_KEY_SIZE;i++) term.tckey[i] = NULL; |
| 389 |
term.tcval = (int*)malloc(sizeof(int)*TC_VAL_SIZE); |
| 390 |
if(term.tcval) |
| 391 |
for(i=0;i<TC_VAL_SIZE;i++) term.tcval[i] = 0; |
| 392 |
|
| 393 |
/* memory allocation error */ |
| 394 |
if(term.env == NULL || term.tcval == NULL || |
| 395 |
term.tcstr == NULL || term.tckey == NULL){ |
| 396 |
sigprocmask(SIG_SETMASK, &oset, NULL); |
| 397 |
return -1; |
| 398 |
} |
| 399 |
|
| 400 |
ent = tgetent(term.tcap, term.env); |
| 401 |
if(ent<=0){ |
| 402 |
if(ent==-1) |
| 403 |
fprintf(term.ferr,"Cannot read termcap database;\n"); |
| 404 |
else if(ent==0) |
| 405 |
fprintf(term.ferr,"No entry for terminal type \"%s\";\n",term.env); |
| 406 |
fprintf(term.ferr,"using dumb terminal settings.\n"); |
| 407 |
/* dumb terminal */ |
| 408 |
term.col = 80; |
| 409 |
term.lin = 0; |
| 410 |
} else { |
| 411 |
term.col = tgetnum("co"); |
| 412 |
term.lin = tgetnum("li"); |
| 413 |
for(i=0; term_val_names[i]!=NULL; i++) |
| 414 |
term.tcval[i] = tgetnum(term_val_names[i]); |
| 415 |
for(i=0; term_key_names[i]!=NULL; i++){ |
| 416 |
tmp = tgetstr(term_key_names[i], &area); |
| 417 |
if(tmp != 0) |
| 418 |
_sl4_tckey_set(i, tmp); |
| 419 |
} |
| 420 |
for(i=0; term_str_names[i]!=NULL; i++){ |
| 421 |
tmp = tgetstr(term_str_names[i], &area); |
| 422 |
if(tmp != 0) |
| 423 |
_sl4_tcstr_set(i, tmp); |
| 424 |
} |
| 425 |
} |
| 426 |
if(term.col < 2) term.col = 80; |
| 427 |
if(term.lin < 1) term.lin = 24; |
| 428 |
|
| 429 |
term.got_sigwinch = 0; |
| 430 |
_sl4_term_refresh_wsize(); |
| 431 |
/* restore SIGWINCH signal */ |
| 432 |
sigprocmask(SIG_SETMASK, &oset, NULL); |
| 433 |
|
| 434 |
/* register SIGWINCH event hander */ |
| 435 |
sigact.sa_handler = sigwinch_handler; |
| 436 |
sigemptyset(&sigact.sa_mask); |
| 437 |
sigact.sa_flags = SA_RESTART; |
| 438 |
sigaction(SIGWINCH, &sigact, NULL); |
| 439 |
return 0; |
| 440 |
} |
| 441 |
|
| 442 |
int sl4_term_quit(){ |
| 443 |
int i; |
| 444 |
_sl4_tty_quit(); |
| 445 |
free(term.env); term.env = NULL; |
| 446 |
for(i=0;i<TC_STR_SIZE;i++) _sl4_tcstr_set(i, NULL); |
| 447 |
free(term.tcstr); term.tcstr = NULL; |
| 448 |
for(i=0;i<TC_KEY_SIZE;i++) _sl4_tckey_set(i, NULL); |
| 449 |
free(term.tckey); term.tckey = NULL; |
| 450 |
/* for(i=0;i<TC_VAL_SIZE;i++) term.tcval[i] = 0; */ |
| 451 |
free(term.tcval); term.tcval = NULL; |
| 452 |
return 0; |
| 453 |
} |
| 454 |
|
| 455 |
int sl4_term_putc(int c){ |
| 456 |
return fputc(c, term.fout); |
| 457 |
} |
| 458 |
|
| 459 |
int sl4_term_puts(const char *str){ |
| 460 |
return fputs(str, term.fout); |
| 461 |
} |
| 462 |
|
| 463 |
int sl4_term_getc(){ |
| 464 |
return fgetc(term.fin); |
| 465 |
} |
| 466 |
|
| 467 |
int sl4_term_keypad_getc(){ |
| 468 |
int i,j,ret,ch,ch2,kp,n; |
| 469 |
const char *p; |
| 470 |
char buf[128]; |
| 471 |
ch = fgetc(term.fin); |
| 472 |
ret = ch; |
| 473 |
for(i=0;i<KEYPAD_SIZE;i++){ |
| 474 |
kp = -1; n = 0; |
| 475 |
p = term_keypad[i].key; |
| 476 |
if(*p == ch){ |
| 477 |
for(p++; *p!='\0'; p++){ |
| 478 |
ch2 = fgetc(term.fin); |
| 479 |
buf[n++] = ch2; |
| 480 |
if(*p != ch2 || n == 128){ |
| 481 |
for(j=n-1;j>=0;j--) ungetc(buf[j], term.fin); |
| 482 |
break; |
| 483 |
} |
| 484 |
} |
| 485 |
if(*p=='\0') kp=term_keypad[i].code; |
| 486 |
} |
| 487 |
if(kp!=-1){ ret = kp; break; } |
| 488 |
} |
| 489 |
if(kp==-1){ |
| 490 |
for(i=0; i<TC_KEY_SIZE; i++){ |
| 491 |
n=0; |
| 492 |
if(TC_KEY_IS_SET(i)) p=TC_KEY(i); |
| 493 |
else continue; |
| 494 |
if(*p == ch){ |
| 495 |
for(p++; *p!='\0'; p++){ |
| 496 |
ch2 = fgetc(term.fin); |
| 497 |
buf[n++] = ch2; |
| 498 |
if(*p != ch2 || n == 128){ |
| 499 |
for(j=n-1; j>=0; j--) ungetc(buf[j], term.fin); |
| 500 |
break; |
| 501 |
} |
| 502 |
} |
| 503 |
if(*p=='\0'){ |
| 504 |
switch(i){ |
| 505 |
case TC_kd: kp=SL4_KEYPAD_DOWN; break; |
| 506 |
case TC_kl: kp=SL4_KEYPAD_LEFT; break; |
| 507 |
case TC_kr: kp=SL4_KEYPAD_RIGHT; break; |
| 508 |
case TC_ku: kp=SL4_KEYPAD_UP; break; |
| 509 |
case TC_kh: kp=SL4_KEYPAD_HOME; break; |
| 510 |
case TC_at7: kp=SL4_KEYPAD_END; break; |
| 511 |
case TC_kb: kp=0x08; break; |
| 512 |
case TC_kD: kp=0x7f; break; |
| 513 |
} |
| 514 |
} |
| 515 |
} |
| 516 |
if(kp!=-1){ ret = kp; break; } |
| 517 |
} |
| 518 |
} |
| 519 |
return ret; |
| 520 |
} |
| 521 |
|
| 522 |
char *sl4_term_gets(char *buf, int size){ |
| 523 |
return fgets(buf, size, term.fin); |
| 524 |
} |
| 525 |
|
| 526 |
int sl4_term_cursor_up(int y){ |
| 527 |
int i; |
| 528 |
if(y <= 0) return -1; |
| 529 |
if(TC_STR_IS_SET(TC_UP)){ |
| 530 |
tputs(tgoto(TC_STR(TC_UP),y,y), y, sl4_term_putc); |
| 531 |
}else if(TC_STR_IS_SET(TC_up)){ |
| 532 |
for(i=0;i<y;i++) |
| 533 |
tputs(TC_STR(TC_up), 1, sl4_term_putc); |
| 534 |
} |
| 535 |
return 0; |
| 536 |
} |
| 537 |
|
| 538 |
int sl4_term_cursor_down(int y){ |
| 539 |
int i; |
| 540 |
if(y <= 0) return -1; |
| 541 |
if(TC_STR_IS_SET(TC_DO)){ |
| 542 |
tputs(tgoto(TC_STR(TC_DO),y,y), y, sl4_term_putc); |
| 543 |
}else if(TC_STR_IS_SET(TC_do)){ |
| 544 |
for(i=0;i<y;i++) |
| 545 |
tputs(TC_STR(TC_do), 1, sl4_term_putc); |
| 546 |
} |
| 547 |
return 0; |
| 548 |
} |
| 549 |
|
| 550 |
int sl4_term_cursor_right(int x){ |
| 551 |
int i; |
| 552 |
if(x <= 0) return -1; |
| 553 |
if(TC_STR_IS_SET(TC_RI)){ |
| 554 |
tputs(tgoto(TC_STR(TC_RI),x,x), x, sl4_term_putc); |
| 555 |
}else if(TC_STR_IS_SET(TC_nd)){ |
| 556 |
for(i=0;i<x;i++) |
| 557 |
tputs(TC_STR(TC_nd), 1, sl4_term_putc); |
| 558 |
} |
| 559 |
return 0; |
| 560 |
} |
| 561 |
|
| 562 |
int sl4_term_cursor_left(int x){ |
| 563 |
int i; |
| 564 |
if(x <= 0) return -1; |
| 565 |
if(TC_STR_IS_SET(TC_LE)){ |
| 566 |
tputs(tgoto(TC_STR(TC_LE),x,x), x, sl4_term_putc); |
| 567 |
}else if(TC_STR_IS_SET(TC_le)){ |
| 568 |
for(i=0;i<x;i++) |
| 569 |
tputs(TC_STR(TC_le), 1, sl4_term_putc); |
| 570 |
} |
| 571 |
return 0; |
| 572 |
} |
| 573 |
|
| 574 |
int sl4_term_cursor_bol(){ |
| 575 |
if(TC_STR_IS_SET(TC_cr)){ |
| 576 |
tputs(TC_STR(TC_cr), 1, sl4_term_putc); |
| 577 |
}else{ |
| 578 |
sl4_term_putc('\r'); |
| 579 |
} |
| 580 |
return 0; |
| 581 |
} |
| 582 |
|
| 583 |
int sl4_term_cursor_newline(){ |
| 584 |
sl4_term_cursor_bol(); |
| 585 |
sl4_term_cursor_right(term.col-1); |
| 586 |
if(TC_STR_IS_SET(TC_nw)){ |
| 587 |
tputs(TC_STR(TC_nw), 1, sl4_term_putc); |
| 588 |
}else{ |
| 589 |
sl4_term_putc('\r'); |
| 590 |
sl4_term_putc('\n'); |
| 591 |
} |
| 592 |
return 0; |
| 593 |
} |
| 594 |
|
| 595 |
int sl4_term_cursor_invisible(){ |
| 596 |
if(TC_STR_IS_SET(TC_vi)){ |
| 597 |
tputs(TC_STR(TC_vi), 1, sl4_term_putc); |
| 598 |
} |
| 599 |
return 0; |
| 600 |
} |
| 601 |
|
| 602 |
int sl4_term_cursor_normal(){ |
| 603 |
if(TC_STR_IS_SET(TC_ve)){ |
| 604 |
tputs(TC_STR(TC_ve), 1, sl4_term_putc); |
| 605 |
} |
| 606 |
return 0; |
| 607 |
} |
| 608 |
|
| 609 |
int sl4_term_bell(){ |
| 610 |
if(TC_STR_IS_SET(TC_bl)){ |
| 611 |
tputs(TC_STR(TC_bl), 1, sl4_term_putc); |
| 612 |
} |
| 613 |
return 0; |
| 614 |
} |
| 615 |
|
| 616 |
int sl4_term_vbell(){ |
| 617 |
if(TC_STR_IS_SET(TC_vb)){ |
| 618 |
tputs(TC_STR(TC_vb), 1, sl4_term_putc); |
| 619 |
} |
| 620 |
return 0; |
| 621 |
} |
| 622 |
|
| 623 |
int sl4_term_attr_normal(){ |
| 624 |
char se,ue,me; |
| 625 |
se=ue=me=0; |
| 626 |
if(TC_STR_IS_SET(TC_se)) se=1; |
| 627 |
if(TC_STR_IS_SET(TC_ue)) ue=1; |
| 628 |
if(TC_STR_IS_SET(TC_me)) me=1; |
| 629 |
if(se && me) if(!strcmp(TC_STR(TC_se), TC_STR(TC_me))) se=0; |
| 630 |
if(ue && me) if(!strcmp(TC_STR(TC_ue), TC_STR(TC_me))) ue=0; |
| 631 |
if(se) tputs(TC_STR(TC_se), 1, sl4_term_putc); |
| 632 |
if(ue) tputs(TC_STR(TC_ue), 1, sl4_term_putc); |
| 633 |
if(me) tputs(TC_STR(TC_me), 1, sl4_term_putc); |
| 634 |
return 0; |
| 635 |
} |
| 636 |
|
| 637 |
int sl4_term_attr_bold(){ |
| 638 |
if(TC_STR_IS_SET(TC_md)) |
| 639 |
tputs(TC_STR(TC_md), 1, sl4_term_putc); |
| 640 |
return 0; |
| 641 |
} |
| 642 |
|
| 643 |
int sl4_term_attr_underline(){ |
| 644 |
if(TC_STR_IS_SET(TC_us)) |
| 645 |
tputs(TC_STR(TC_us), 1, sl4_term_putc); |
| 646 |
return 0; |
| 647 |
} |
| 648 |
|
| 649 |
int sl4_term_attr_reverse(){ |
| 650 |
if(TC_STR_IS_SET(TC_so)) |
| 651 |
tputs(TC_STR(TC_so), 1, sl4_term_putc); |
| 652 |
return 0; |
| 653 |
} |
| 654 |
|
| 655 |
int sl4_term_clear_screen(){ |
| 656 |
if(TC_STR_IS_SET(TC_cl)){ |
| 657 |
tputs(TC_STR(TC_cl), term.lin, sl4_term_putc); |
| 658 |
}else if(TC_STR_IS_SET(TC_ho) && (TC_STR_IS_SET(TC_cd))){ |
| 659 |
tputs(TC_STR(TC_ho), term.lin, sl4_term_putc); |
| 660 |
tputs(TC_STR(TC_cd), term.lin, sl4_term_putc); |
| 661 |
}else{ |
| 662 |
sl4_term_putc('\r'); |
| 663 |
sl4_term_putc('\n'); |
| 664 |
} |
| 665 |
return 0; |
| 666 |
} |
| 667 |
|
| 668 |
int sl4_term_clear_eol(int xpos){ |
| 669 |
int i,dis; |
| 670 |
if(TC_STR_IS_SET(TC_ce)){ |
| 671 |
tputs(TC_STR(TC_ce), 1, sl4_term_putc); |
| 672 |
}else{ |
| 673 |
dis=term.col - xpos; |
| 674 |
for(i=0;i<dis;i++) sl4_term_putc(' '); |
| 675 |
sl4_term_cursor_left(dis-1); |
| 676 |
} |
| 677 |
return 0; |
| 678 |
} |
| 679 |
|
| 680 |
int sl4_term_getmaxyx(int *y, int *x){ |
| 681 |
if(term.got_sigwinch){ |
| 682 |
_sl4_term_refresh_wsize(); |
| 683 |
term.got_sigwinch = 0; |
| 684 |
} |
| 685 |
if(y != NULL) *y=term.lin; |
| 686 |
if(x != NULL) *x=term.col; |
| 687 |
return 0; |
| 688 |
} |
| 689 |
|
| 690 |
|
| 691 |
#ifdef __cplusplus |
| 692 |
} |
| 693 |
#endif |
| 694 |
|
| 695 |
#endif /* WIN32 */ |
| 696 |
|