| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#ifdef GLK |
| 8 |
|
| 9 |
#include "readline.h" |
| 10 |
#include "main.h" |
| 11 |
#include "glk.h" |
| 12 |
#include "glkio.h" |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
Boolean readline(char usrbuf[]) |
| 24 |
{ |
| 25 |
event_t event; |
| 26 |
glk_request_line_event(glkMainWin, usrbuf, 255, 0); |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
do |
| 31 |
{ |
| 32 |
glk_select(&event); |
| 33 |
if (evtype_Arrange == event.type) |
| 34 |
statusline(); |
| 35 |
} while (event.type != evtype_LineInput); |
| 36 |
usrbuf[event.val1] = 0; |
| 37 |
return TRUE; |
| 38 |
} |
| 39 |
|
| 40 |
#else |
| 41 |
|
| 42 |
#include "sysdep.h" |
| 43 |
#include <stdlib.h> |
| 44 |
#include <stdio.h> |
| 45 |
#include <ctype.h> |
| 46 |
|
| 47 |
#ifdef HAVE_TERMIO |
| 48 |
#include <termios.h> |
| 49 |
#endif |
| 50 |
|
| 51 |
#ifdef __PACIFIC__ |
| 52 |
#include <unixio.h> |
| 53 |
#else |
| 54 |
#include <unistd.h> |
| 55 |
#endif |
| 56 |
|
| 57 |
#include "readline.h" |
| 58 |
|
| 59 |
#include "main.h" |
| 60 |
|
| 61 |
|
| 62 |
#ifdef HAVE_TERMIO |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
static struct termios term; |
| 70 |
|
| 71 |
static void newtermio() |
| 72 |
{ |
| 73 |
struct termios newterm; |
| 74 |
tcgetattr(0, &term); |
| 75 |
newterm=term; |
| 76 |
newterm.c_lflag&=~(ECHO|ICANON); |
| 77 |
newterm.c_cc[VMIN]=1; |
| 78 |
newterm.c_cc[VTIME]=0; |
| 79 |
tcsetattr(0, TCSANOW, &newterm); |
| 80 |
} |
| 81 |
|
| 82 |
static void restoretermio() |
| 83 |
{ |
| 84 |
tcsetattr(0, TCSANOW, &term); |
| 85 |
} |
| 86 |
|
| 87 |
#endif |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
static unsigned char buffer[LINELENGTH+1]; |
| 97 |
static int bufidx; |
| 98 |
|
| 99 |
static unsigned char *history[HISTORYLENGTH]; |
| 100 |
static int histidx; |
| 101 |
static int histp; |
| 102 |
|
| 103 |
static unsigned char ch; |
| 104 |
static int endOfInput = 0; |
| 105 |
static Boolean change; |
| 106 |
static Boolean insert = TRUE; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
typedef struct {unsigned char min, max; void (*hook)(char ch);} KeyMap; |
| 116 |
|
| 117 |
|
| 118 |
static void escHook(char ch); |
| 119 |
static void insertCh(char ch); |
| 120 |
static void arrowHook(char ch); |
| 121 |
static void upArrow(char ch); |
| 122 |
static void downArrow(char ch); |
| 123 |
static void rightArrow(char ch); |
| 124 |
static void leftArrow(char ch); |
| 125 |
static void insertToggle(char ch); |
| 126 |
static void newLine(char ch); |
| 127 |
static void delFwd(char ch); |
| 128 |
static void delBwd(char ch); |
| 129 |
|
| 130 |
#ifdef __XXunix__ |
| 131 |
static KeyMap keymap[] = { |
| 132 |
{0x00, 0x07, NULL}, |
| 133 |
{0x08, 0x08, delBwd}, |
| 134 |
{0x09, 0x09, NULL}, |
| 135 |
{0x0a, 0x0a, newLine}, |
| 136 |
{0x1b, 0x1b, escHook}, |
| 137 |
{0x1c, 0x7e, insertCh}, |
| 138 |
{0x7f, 0x7f, delFwd}, |
| 139 |
{0x80, 0xff, insertCh}, |
| 140 |
{0x00, 0x00, NULL} |
| 141 |
}; |
| 142 |
|
| 143 |
|
| 144 |
#ifdef __solarisX__ |
| 145 |
static KeyMap escmap[] = { |
| 146 |
{0x00, 0x4e, NULL}, |
| 147 |
{0x4f, 0x4f, arrowHook}, |
| 148 |
{0x50, 0xff, NULL}, |
| 149 |
{0x00, 0x00, NULL} |
| 150 |
}; |
| 151 |
#else |
| 152 |
static KeyMap escmap[] = { |
| 153 |
{0x00, 0x5a, NULL}, |
| 154 |
{0x5b, 0x5b, arrowHook}, |
| 155 |
{0x5c, 0xff, NULL}, |
| 156 |
{0x00, 0x00, NULL} |
| 157 |
}; |
| 158 |
#endif |
| 159 |
|
| 160 |
static KeyMap arrowmap[] = { |
| 161 |
{0x00, 0x31, NULL}, |
| 162 |
{0x32, 0x32, insertToggle}, |
| 163 |
{0x33, 0x40, NULL}, |
| 164 |
{0x41, 0x41, upArrow}, |
| 165 |
{0x42, 0x42, downArrow}, |
| 166 |
{0x43, 0x43, rightArrow}, |
| 167 |
{0x44, 0x44, leftArrow}, |
| 168 |
{0x45, 0xff, NULL}, |
| 169 |
{0x00, 0x00, NULL} |
| 170 |
}; |
| 171 |
|
| 172 |
#endif |
| 173 |
|
| 174 |
#ifdef __win__ |
| 175 |
static KeyMap keymap[] = { |
| 176 |
{0x00, 0x01, NULL}, |
| 177 |
{0x02, 0x02, leftArrow}, |
| 178 |
{0x03, 0x05, NULL}, |
| 179 |
{0x06, 0x06, rightArrow}, |
| 180 |
{0x07, 0x07, NULL}, |
| 181 |
{0x08, 0x08, delBwd}, |
| 182 |
{0x09, 0x09, NULL}, |
| 183 |
{0x0a, 0x0a, newLine}, |
| 184 |
{0x1b, 0x1b, escHook}, |
| 185 |
{0x1c, 0x7e, insertCh}, |
| 186 |
{0x7f, 0x7f, delFwd}, |
| 187 |
{0x80, 0xff, insertCh}, |
| 188 |
{0x00, 0x00, NULL} |
| 189 |
}; |
| 190 |
|
| 191 |
static KeyMap escmap[] = { |
| 192 |
{0x00, 0x5a, NULL}, |
| 193 |
{0x5b, 0x5b, arrowHook}, |
| 194 |
{0x5c, 0xff, NULL}, |
| 195 |
{0x00, 0x00, NULL} |
| 196 |
}; |
| 197 |
|
| 198 |
static KeyMap arrowmap[] = { |
| 199 |
{0x00, 0x31, NULL}, |
| 200 |
{0x32, 0x32, insertToggle}, |
| 201 |
{0x33, 0x40, NULL}, |
| 202 |
{0x41, 0x41, upArrow}, |
| 203 |
{0x42, 0x42, downArrow}, |
| 204 |
{0x43, 0x43, rightArrow}, |
| 205 |
{0x44, 0x44, leftArrow}, |
| 206 |
{0x45, 0xff, NULL}, |
| 207 |
{0x00, 0x00, NULL} |
| 208 |
}; |
| 209 |
|
| 210 |
#endif |
| 211 |
|
| 212 |
#ifdef __dos__ |
| 213 |
static KeyMap keymap[] = { |
| 214 |
{0x00, 0x01, NULL}, |
| 215 |
{0x02, 0x02, leftArrow}, |
| 216 |
{0x03, 0x05, NULL}, |
| 217 |
{0x06, 0x06, rightArrow}, |
| 218 |
{0x07, 0x07, NULL}, |
| 219 |
{0x08, 0x08, delBwd}, |
| 220 |
{0x09, 0x09, NULL}, |
| 221 |
{0x0a, 0x0a, newLine}, |
| 222 |
{0x1b, 0x1b, escHook}, |
| 223 |
{0x1c, 0x7e, insertCh}, |
| 224 |
{0x7f, 0x7f, delFwd}, |
| 225 |
{0x80, 0xff, insertCh}, |
| 226 |
{0x00, 0x00, NULL} |
| 227 |
}; |
| 228 |
|
| 229 |
static KeyMap escmap[] = { |
| 230 |
{0x00, 0x5a, NULL}, |
| 231 |
{0x5b, 0x5b, arrowHook}, |
| 232 |
{0x5c, 0xff, NULL}, |
| 233 |
{0x00, 0x00, NULL} |
| 234 |
}; |
| 235 |
|
| 236 |
static KeyMap arrowmap[] = { |
| 237 |
{0x00, 0x31, NULL}, |
| 238 |
{0x32, 0x32, insertToggle}, |
| 239 |
{0x33, 0x40, NULL}, |
| 240 |
{0x41, 0x41, upArrow}, |
| 241 |
{0x42, 0x42, downArrow}, |
| 242 |
{0x43, 0x43, rightArrow}, |
| 243 |
{0x44, 0x44, leftArrow}, |
| 244 |
{0x45, 0xff, NULL}, |
| 245 |
{0x00, 0x00, NULL} |
| 246 |
}; |
| 247 |
|
| 248 |
#endif |
| 249 |
|
| 250 |
|
| 251 |
static void doBeep(void) |
| 252 |
{ |
| 253 |
write(1, "\7", 1); |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
static void backspace(void) |
| 258 |
{ |
| 259 |
write(1, "\b", 1); |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
static void erase() |
| 264 |
{ |
| 265 |
int i; |
| 266 |
|
| 267 |
for (i = 0; i < bufidx; i++) backspace(); |
| 268 |
for (i = 0; i < strlen((char *)buffer); i++) write(1, " ", 1); |
| 269 |
for (i = 0; i < strlen((char *)buffer); i++) backspace(); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
static void execute(KeyMap map[], unsigned char ch) |
| 279 |
{ |
| 280 |
int i = 0; |
| 281 |
|
| 282 |
for (i = 0; i <= 256; i++) { |
| 283 |
if (i > 0 && map[i].min == 0x00) break; |
| 284 |
if (map[i].min <= ch && ch <= map[i].max) { |
| 285 |
if (map[i].hook != NULL) { |
| 286 |
map[i].hook(ch); |
| 287 |
return; |
| 288 |
} else |
| 289 |
doBeep(); |
| 290 |
} |
| 291 |
} |
| 292 |
doBeep(); |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
static void upArrow(char ch) |
| 297 |
{ |
| 298 |
|
| 299 |
if (history[(histp+HISTORYLENGTH-1)%HISTORYLENGTH] == NULL || |
| 300 |
(histp+HISTORYLENGTH-1)%HISTORYLENGTH == histidx) { |
| 301 |
write(1, "\7", 1); |
| 302 |
return; |
| 303 |
} |
| 304 |
|
| 305 |
erase(); |
| 306 |
|
| 307 |
|
| 308 |
histp = (histp+HISTORYLENGTH-1)%HISTORYLENGTH; |
| 309 |
|
| 310 |
|
| 311 |
strcpy((char *)buffer, (char *)history[histp]); |
| 312 |
bufidx = strlen((char *)buffer); |
| 313 |
write(1, (void *)buffer, strlen((char *)buffer)); |
| 314 |
|
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
static void downArrow(char ch) |
| 319 |
{ |
| 320 |
|
| 321 |
if (histp == histidx) { |
| 322 |
write(1, "\7", 1); |
| 323 |
return; |
| 324 |
} |
| 325 |
|
| 326 |
erase(); |
| 327 |
|
| 328 |
|
| 329 |
histp = (histp+1)%HISTORYLENGTH; |
| 330 |
|
| 331 |
|
| 332 |
if (histp != histidx) { |
| 333 |
strcpy((char *)buffer, (char *)history[histp]); |
| 334 |
bufidx = strlen((char *)buffer); |
| 335 |
write(1, (void *)buffer, strlen((char *)buffer)); |
| 336 |
} else { |
| 337 |
bufidx = 0; |
| 338 |
buffer[0] = '\0'; |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
static void rightArrow(char ch) |
| 344 |
{ |
| 345 |
if (bufidx > LINELENGTH || buffer[bufidx] == '\0') |
| 346 |
doBeep(); |
| 347 |
else { |
| 348 |
write(1, (void *)&buffer[bufidx], 1); |
| 349 |
bufidx++; |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
static void leftArrow(char ch) |
| 355 |
{ |
| 356 |
if (bufidx == 0) |
| 357 |
doBeep(); |
| 358 |
else { |
| 359 |
bufidx--; |
| 360 |
backspace(); |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
static void insertToggle(char ch) |
| 366 |
{ |
| 367 |
read(0, &ch, 1); |
| 368 |
if (ch != 'z') |
| 369 |
doBeep(); |
| 370 |
else |
| 371 |
insert = !insert; |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
static void delBwd(char ch) |
| 376 |
{ |
| 377 |
if (bufidx == 0) |
| 378 |
doBeep(); |
| 379 |
else { |
| 380 |
int i; |
| 381 |
|
| 382 |
change = TRUE; |
| 383 |
backspace(); |
| 384 |
bufidx--; |
| 385 |
for (i = 0; i <= strlen((char *)&buffer[bufidx+1]); i++) |
| 386 |
buffer[bufidx+i] = buffer[bufidx+1+i]; |
| 387 |
write(1, (void *)&buffer[bufidx], strlen((char *)&buffer[bufidx])); |
| 388 |
write(1, " ", 1); |
| 389 |
for (i = 0; i <= strlen((char *)&buffer[bufidx]); i++) backspace(); |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
static void delFwd(char ch) |
| 394 |
{ |
| 395 |
if (bufidx > LINELENGTH || buffer[bufidx] == '\0') |
| 396 |
doBeep(); |
| 397 |
else { |
| 398 |
int i; |
| 399 |
|
| 400 |
change = TRUE; |
| 401 |
strcpy((char *)&buffer[bufidx], (char *)&buffer[bufidx+1]); |
| 402 |
write(1, (void *)&buffer[bufidx], strlen((char *)&buffer[bufidx])); |
| 403 |
write(1, " ", 1); |
| 404 |
for (i = 0; i <= strlen((char *)&buffer[bufidx]); i++) backspace(); |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
static void escHook(char ch) { |
| 409 |
read(0, &ch, 1); |
| 410 |
execute(escmap, ch); |
| 411 |
} |
| 412 |
|
| 413 |
static void arrowHook(char ch) { |
| 414 |
read(0, &ch, 1); |
| 415 |
execute(arrowmap, ch); |
| 416 |
} |
| 417 |
|
| 418 |
static void newLine(char ch) |
| 419 |
{ |
| 420 |
endOfInput = 1; |
| 421 |
write(1, "\n", 1); |
| 422 |
|
| 423 |
|
| 424 |
if (change && strlen((char *)buffer) > 0) { |
| 425 |
if (history[histidx] == NULL) |
| 426 |
history[histidx] = (unsigned char *)allocate(LINELENGTH+1); |
| 427 |
strcpy((char *)history[histidx], (char *)buffer); |
| 428 |
histidx = (histidx+1)%HISTORYLENGTH; |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
static void insertCh(char ch) { |
| 434 |
if (bufidx > LINELENGTH) |
| 435 |
doBeep(); |
| 436 |
else { |
| 437 |
|
| 438 |
if (buffer[bufidx] == '\0') |
| 439 |
buffer[bufidx+1] = '\0'; |
| 440 |
else if (insert) { |
| 441 |
int i; |
| 442 |
|
| 443 |
|
| 444 |
for (i = strlen((char *)buffer); i >= bufidx; i--) |
| 445 |
buffer[i+1] = buffer[i]; |
| 446 |
write(1, (void *)&buffer[bufidx], strlen((char *)&buffer[bufidx])); |
| 447 |
for (i = strlen((char *)&buffer[bufidx]); i > 0; i--) backspace(); |
| 448 |
} |
| 449 |
change = TRUE; |
| 450 |
buffer[bufidx] = ch; |
| 451 |
write(1, &ch, 1); |
| 452 |
bufidx++; |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
static void echoOff() |
| 463 |
{ |
| 464 |
#ifdef HAVE_TERMIO |
| 465 |
newtermio(); |
| 466 |
#else |
| 467 |
#ifdef __win__ |
| 468 |
#include <windows.h> |
| 469 |
#include <winbase.h> |
| 470 |
#include <wincon.h> |
| 471 |
|
| 472 |
DWORD handle = GetStdHandle(STD_INPUT_HANDLE); |
| 473 |
|
| 474 |
(void) SetConsoleMode(handle, 0); |
| 475 |
|
| 476 |
#endif |
| 477 |
#endif |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
static void echoOn() |
| 487 |
{ |
| 488 |
#ifdef HAVE_TERMIO |
| 489 |
restoretermio(); |
| 490 |
#else |
| 491 |
#ifdef __win__ |
| 492 |
#include <windows.h> |
| 493 |
#include <winbase.h> |
| 494 |
#include <wincon.h> |
| 495 |
|
| 496 |
DWORD handle = GetStdHandle(STD_INPUT_HANDLE); |
| 497 |
(void) SetConsoleMode(handle, ENABLE_ECHO_INPUT); |
| 498 |
|
| 499 |
#endif |
| 500 |
#endif |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
Boolean readline(char usrbuf[]) |
| 514 |
{ |
| 515 |
fflush(stdout); |
| 516 |
bufidx = 0; |
| 517 |
histp = histidx; |
| 518 |
buffer[0] = '\0'; |
| 519 |
change = TRUE; |
| 520 |
echoOff(); |
| 521 |
endOfInput = 0; |
| 522 |
while (!endOfInput) { |
| 523 |
if (read(0, (void *)&ch, 1) != 1) { |
| 524 |
echoOn(); |
| 525 |
return FALSE; |
| 526 |
} |
| 527 |
execute(keymap, ch); |
| 528 |
} |
| 529 |
echoOn(); |
| 530 |
strcpy(usrbuf, (char *)buffer); |
| 531 |
return TRUE; |
| 532 |
} |
| 533 |
|
| 534 |
#endif |