Debian Codegraph @

terps/alan3/readline.c

raw symbols SCIP
1 /*----------------------------------------------------------------------
2
3 Read line from user, with editing and history
4
5 ----------------------------------------------------------------------*/
6
7 #include "readline.h"
8
9 #include "sysdep.h"
10 #include "output.h"
11 #include "term.h"
12 #include "exe.h"
13 #include "save.h"
14 #include "Location.h"
15 #include "converter.h"
16
17 #include "options.h"
18
19
20 #define LINELENGTH 1000
21
22 #ifdef UNITTESTING
23 /* Transform some stdlib functions to mockable functions */
24 #define read(fd, buf, n) mocked_read(fd, buf, n)
25 extern ssize_t mocked_read (int __fd, char *__buf, size_t __nbytes);
26
27 #define write(fd, buf, n) mocked_write(fd, buf, n)
28 extern ssize_t write (int __fd, const void *__buf, size_t __n);
29
30 #endif
31
32
33 // TODO Try to split this into more obvious GLK and non-GLK modules
34 #ifdef HAVE_GLK
35
36 #include "options.h"
37
38 #include "glk.h"
39 #include "glkio.h"
40
41 #include "resources.h"
42
43
44 #ifdef HAVE_WINGLK
45 #include "WinGlk.h"
46
47 extern HINSTANCE myInstance; /* Catched by winglk.c */
48
49
50 BOOL CALLBACK AboutDialogProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
51 {
52 switch (message) {
53 case WM_COMMAND:
54 switch (LOWORD(wParam)) {
55 case IDOK:
56 EndDialog(hwndDlg, wParam);
57 return true;
58 }
59 }
60 return false;
61 }
62
63 #endif
64
65
66
67 /*======================================================================
68
69 readline() - GLK case
70
71 Read a line from the user, with history and editing
72
73 */
74
75 /* TODO - length of user buffer should be used */
76 bool readline(char buffer[])
77 {
78 event_t event;
79 static bool readingCommands = false;
80 static frefid_t commandFileRef;
81 static strid_t commandFile;
82 #ifdef HAVE_WINGLK
83 static frefid_t commandLogFileRef;
84 static frefid_t transcriptFileRef;
85 INT_PTR e;
86 #endif
87
88 if (readingCommands) {
89 if (glk_get_line_stream(commandFile, buffer, 255) == 0) {
90 glk_stream_close(commandFile, NULL);
91 readingCommands = false;
92 goto endOfCommandFile;
93 } else {
94 char *converted = ensureInternalEncoding(buffer);
95 glk_set_style(style_Input);
96 printf(converted);
97 glk_set_style(style_Normal);
98 free(converted);
99 }
100 } else {
101 endOfCommandFile:
102 glk_request_line_event(glkMainWin, buffer, 255, 0);
103 /* FIXME: buffer size should be infallible: all existing calls use 256 or
104 80 character buffers, except parse which uses LISTLEN (currently 100)
105 */
106 do
107 {
108 glk_select(&event);
109 switch (event.type) {
110 case evtype_Arrange:
111 statusline();
112 break;
113 #ifdef HAVE_WINGLK
114 case winglk_evtype_GuiInput:
115 switch (event.val1) {
116 case ID_MENU_RESTART:
117 restartGame();
118 break;
119 case ID_MENU_SAVE:
120 glk_set_style(style_Input);
121 printf("save\n");
122 glk_set_style(style_Normal);
123 save();
124 para();
125 printf("> ");
126 break;
127 case ID_MENU_RESTORE:
128 glk_set_style(style_Input);
129 printf("restore\n");
130 glk_set_style(style_Normal);
131 restore();
132 look();
133 para();
134 printf("> ");
135 break;
136 case ID_MENU_RECORD:
137 if (commandLogOption) {
138 glk_stream_close(commandLogFile, NULL);
139 commandLogOption = false;
140 }
141 commandLogFileRef = glk_fileref_create_by_prompt(fileusage_InputRecord+fileusage_TextMode, filemode_Write, 0);
142 if (commandLogFileRef == NULL) break;
143 commandLogFile = glk_stream_open_file(commandLogFileRef, filemode_Write, 0);
144 if (commandLogFile != NULL)
145 commandLogOption = true;
146 break;
147 case ID_MENU_PLAYBACK:
148 commandFileRef = glk_fileref_create_by_prompt(fileusage_InputRecord+fileusage_TextMode, filemode_Read, 0);
149 if (commandFileRef == NULL) break;
150 commandFile = glk_stream_open_file(commandFileRef, filemode_Read, 0);
151 if (commandFile != NULL)
152 if (glk_get_line_stream(commandFile, buffer, 255) != 0) {
153 readingCommands = true;
154 printf(buffer);
155 return true;
156 }
157 break;
158 case ID_MENU_TRANSCRIPT:
159 if (transcriptOption) {
160 glk_stream_close(transcriptFile, NULL);
161 transcriptOption = false;
162 }
163 transcriptFileRef = glk_fileref_create_by_prompt(fileusage_Transcript+fileusage_TextMode, filemode_Write, 0);
164 if (transcriptFileRef == NULL) break;
165 transcriptFile = glk_stream_open_file(transcriptFileRef, filemode_Write, 0);
166 if (transcriptFile != NULL) {
167 transcriptOption = true;
168 glk_put_string_stream(transcriptFile, "> ");
169 }
170 break;
171 case ID_MENU_ABOUT:
172 e = DialogBox(myInstance, MAKEINTRESOURCE(IDD_ABOUT), NULL, &AboutDialogProc);
173 (void)e;
174 break;
175 }
176 break;
177 #endif
178 }
179 } while (event.type != evtype_LineInput);
180 if (buffer[0] == '@') {
181 buffer[event.val1] = 0;
182 commandFileRef = glk_fileref_create_by_name(fileusage_InputRecord+fileusage_TextMode, &buffer[1], 0);
183 commandFile = glk_stream_open_file(commandFileRef, filemode_Read, 0);
184 if (commandFile != NULL)
185 if (glk_get_line_stream(commandFile, buffer, 255) != 0) {
186 char *converted = ensureInternalEncoding(buffer);
187 glk_set_style(style_Input);
188 printf(converted);
189 glk_set_style(style_Normal);
190 free(converted);
191 readingCommands = true;
192 }
193 } else
194 buffer[event.val1] = 0;
195 }
196 return true;
197 }
198
199 #else
200 /*---------------------------------------------------------------------------------------*/
201 /* Non-GLK terminal I/O using simple movements and overwrites with spaces and backspaces */
202 /*---------------------------------------------------------------------------------------*/
203
204 #include "sysdep.h"
205 #include <stdlib.h>
206 #include <stdio.h>
207 #include <ctype.h>
208
209 #ifdef HAVE_TERMIOS
210 #include <termios.h>
211 #endif
212
213 #include <unistd.h>
214
215 #include "memory.h"
216
217
218 #ifdef HAVE_TERMIOS
219 /*----------------------------------------------------------------------
220
221 termio handling
222
223 ----------------------------------------------------------------------*/
224
225 static struct termios term;
226
227 static void newtermio()
228 {
229 if (isatty(STDIN_FILENO)) {
230 struct termios newterm;
231 tcgetattr(STDIN_FILENO, &term);
232 newterm=term;
233 newterm.c_lflag&=~(ECHO|ICANON);
234 newterm.c_cc[VMIN]=1;
235 newterm.c_cc[VTIME]=0;
236 tcsetattr(STDIN_FILENO, TCSANOW, &newterm);
237 }
238 }
239
240 static void restoretermio()
241 {
242 if (isatty(STDIN_FILENO))
243 tcsetattr(STDIN_FILENO, TCSANOW, &term);
244 }
245
246 #endif
247
248
249 /*----------------------------------------------------------------------*\
250
251 Global character buffers etc.
252
253 ----------------------------------------------------------------------*/
254
255 static char buffer[LINELENGTH+1];
256 static int bufidx;
257
258 static unsigned char *history[HISTORYLENGTH];
259 static int histidx; /* Index where to store next history */
260 static int histp; /* Points to the history recalled last */
261
262 static unsigned char ch;
263 static bool endOfInput;
264 static bool commandLineChanged;
265 static bool insertMode = true;
266
267
268 /*----------------------------------------------------------------------
269
270 Character map types and maps
271
272 ----------------------------------------------------------------------*/
273
274 typedef struct {unsigned char min, max; void (*hook)(char ch);} KeyMap;
275
276 /* Forward declaration of hooks */
277 static void escHook(char ch);
278 static void normalCh(char ch);
279 static void arrowHook(char ch);
280 static void upArrow(char ch);
281 static void downArrow(char ch);
282 static void rightArrow(char ch);
283 static void leftArrow(char ch);
284 static void insertToggle(char ch);
285 static void newLine(char ch);
286 static void delFwd(char ch);
287 static void delBwd(char ch);
288 static void execute(KeyMap map[], unsigned char ch);
289
290 #ifdef __windows__
291
292 static KeyMap keymap[] = {
293 {0x00, 0x01, NULL},
294 {0x02, 0x02, leftArrow},
295 {0x03, 0x05, NULL},
296 {0x06, 0x06, rightArrow},
297 {0x07, 0x07, NULL},
298 {0x08, 0x08, delBwd},
299 {0x09, 0x09, NULL},
300 {0x0a, 0x0a, newLine},
301 {0x1b, 0x1b, escHook},
302 {0x1c, 0x7e, normalCh},
303 {0x7f, 0x7f, delFwd},
304 {0x80, 0xff, normalCh},
305 {0x00, 0x00, NULL}
306 };
307
308 static KeyMap escmap[] = {
309 {0x00, 0x5a, NULL},
310 {0x5b, 0x5b, arrowHook},
311 {0x5c, 0xff, NULL},
312 {0x00, 0x00, NULL}
313 };
314
315 static KeyMap arrowmap[] = {
316 {0x00, 0x31, NULL},
317 {0x32, 0x32, insertToggle},
318 {0x33, 0x40, NULL},
319 {0x41, 0x41, upArrow},
320 {0x42, 0x42, downArrow},
321 {0x43, 0x43, rightArrow},
322 {0x44, 0x44, leftArrow},
323 {0x45, 0xff, NULL},
324 {0x00, 0x00, NULL}
325 };
326
327 #else
328
329 /* Not windows */
330
331 static void escapeBracket3Hook(char ch);
332 static void ignoreCh(char ch) {}
333
334 static KeyMap keymap[] = {
335 {0x00, 0x03, NULL},
336 {0x04, 0x04, delFwd},
337 {0x05, 0x07, delBwd},
338 {0x08, 0x08, delBwd},
339 {0x09, 0x09, NULL},
340 {0x0a, 0x0a, newLine},
341 {0x0d, 0x0d, ignoreCh},
342 {0x1b, 0x1b, escHook},
343 {0x1c, 0x7e, normalCh},
344 {0x7f, 0x7f, delBwd},
345 {0x80, 0xff, normalCh},
346 {0x00, 0x00, NULL}
347 };
348
349 static KeyMap escmap[] = {
350 {0x00, 0x5a, NULL},
351 {0x5b, 0x5b, arrowHook},
352 {0x5c, 0xff, NULL},
353 {0x00, 0x00, NULL}
354 };
355
356 static KeyMap arrowmap[] = {
357 {0x00, 0x31, NULL},
358 {0x32, 0x32, insertToggle},
359 {0x33, 0x33, escapeBracket3Hook},
360 {0x33, 0x40, NULL},
361 {0x41, 0x41, upArrow},
362 {0x42, 0x42, downArrow},
363 {0x43, 0x43, rightArrow},
364 {0x44, 0x44, leftArrow},
365 {0x45, 0xff, NULL},
366 {0x00, 0x00, NULL}
367 };
368
369 static KeyMap escapeBracket3map[] = {
370 {0x7e, 0x7e, delFwd},
371 {0x00, 0x00, NULL}
372 };
373
374
375 static void escapeBracket3Hook(char ch) {
376 int rc;
377 (void)rc; /* UNUSED */
378 rc = read(STDIN_FILENO, &ch, 1);
379 execute(escapeBracket3map, ch);
380 }
381
382 #endif
383
384
385 static void stripNewline(char *buffer) {
386 int len = strlen(buffer);
387 if (len > 0 && buffer[len-1] == '\n')
388 buffer[len-1] = '\0';
389 }
390
391 static bool is_utf8_prefix(uchar ch) {
392 return (ch&0xC0) == 0xC0;
393 }
394
395 static bool is_utf8_follow(uchar ch) {
396 return (ch&0xC0) == 0x80;
397 }
398
399 /* Number of characters counting multi-byte characters as one (as opposed to bytes) in a null-terminated array */
400 int character_length(uchar *string) {
401 int count = 0;
402
403 for (int i = 0; string[i] != '\0'; i++) {
404 if (encodingOption == ENCODING_UTF && is_utf8_prefix(string[i])) {
405 count++;
406 i++;
407 do {
408 i++;
409 } while ((string[i] != '\0') && is_utf8_follow(string[i]));
410 i--;
411 } else {
412 count++;
413 }
414 }
415 return count;
416 }
417
418 /* Number of bytes (as opposed to characters e.g. UTF-8) in a null-terminated array */
419 static int byte_length(char *bytes) {
420 return strlen(bytes);
421 }
422
423
424
425 #ifdef UNITTESTING
426 #include <cgreen/mocks.h>
427 static void doBeep(void) { mock(); }
428 #else
429 static void doBeep(void)
430 {
431 int rc;
432 (void)rc; /* UNUSED */
433 rc = write(1, "\7", 1);
434 }
435 #endif
436
437 static void moveCursorLeft(void)
438 {
439 int rc;
440 (void)rc; /* UNUSED */
441 rc = write(1, "\b", 1);
442 }
443
444
445 static void writeBlank() {
446 int rc;
447 (void)rc; /* UNUSED */
448 rc = write(1, " ", 1);
449 }
450
451
452
453 static void erase()
454 {
455 int i;
456
457 /* Backup to beginning of text */
458 /* TODO: moveCursorBackOver(n); including UTF-8 chars */
459 for (i = bufidx; i > 0; i--) {
460 if (encodingOption != ENCODING_UTF || !is_utf8_follow(buffer[i]))
461 moveCursorLeft();
462 }
463
464 /* Overwrite with spaces */
465 for (i = 0; i < character_length((uchar *)buffer); i++) {
466 writeBlank();
467 }
468
469 /* Backup to beginning of text */
470 for (i = 0; i < character_length((uchar *)buffer); i++)
471 moveCursorLeft();
472 }
473
474 /*----------------------------------------------------------------------*\
475
476 Character handling hook functions
477
478 ----------------------------------------------------------------------*/
479
480 static void execute(KeyMap map[], unsigned char ch)
481 {
482 int i = 0;
483
484 // TODO Make this loop until end of KeyMap (0,0,NULL) is found instead
485 // then we can remove any NULL entries in the keymaps
486 for (i = 0; i <= 256; i++) {
487 if (i > 0 && map[i].min == 0x00) break; /* End marker is a 0,0,NULL */
488 if (map[i].min <= ch && ch <= map[i].max) {
489 if (map[i].hook != NULL) {
490 map[i].hook(ch);
491 return;
492 } else
493 doBeep();
494 }
495 }
496 doBeep();
497 }
498
499
500 static void upArrow(char ch)
501 {
502 int rc;
503 (void)rc; /* UNUSED */
504 /* Is there more history ? */
505 if (history[(histp+HISTORYLENGTH-1)%HISTORYLENGTH] == NULL ||
506 (histp+HISTORYLENGTH-1)%HISTORYLENGTH == histidx) {
507 rc = write(1, "\7", 1);
508 return;
509 }
510
511 erase();
512
513 /* Backup history pointer */
514 histp = (histp+HISTORYLENGTH-1)%HISTORYLENGTH;
515
516 /* Copy the history and write it */
517 strcpy((char *)buffer, (char *)history[histp]);
518 bufidx = strlen((char *)buffer);
519 rc = write(1, (void *)buffer, strlen((char *)buffer));
520 }
521
522
523 static void downArrow(char ch)
524 {
525 /* Is there more history ? */
526 if (histp == histidx) {
527 int rc;
528 (void)rc; /* UNUSED */
529 rc = write(1, "\7", 1);
530 return;
531 }
532
533 erase();
534
535 /* Advance history pointer */
536 histp = (histp+1)%HISTORYLENGTH;
537
538 /* If we are not at the most recent history entry, copy history and write it */
539 if (histp != histidx) {
540 int rc;
541 (void)rc; /* UNUSED */
542 strcpy((char *)buffer, (char *)history[histp]);
543 bufidx = strlen((char *)buffer);
544 rc = write(1, (void *)buffer, strlen((char *)buffer));
545 } else {
546 bufidx = 0;
547 buffer[0] = '\0';
548 }
549 }
550
551
552 static int byteLengthOfCharacterAt(int bufidx) {
553 int count = 1;
554 if (encodingOption == ENCODING_UTF) {
555 if (is_utf8_prefix((uchar)buffer[bufidx])) {
556 /* Next character is a multi-byte */
557 while (is_utf8_follow((uchar)buffer[bufidx+1])) {
558 count++;
559 bufidx++;
560 }
561 }
562 }
563
564 return count;
565 }
566
567
568
569 static void rightArrow(char ch)
570 {
571 if (bufidx > LINELENGTH || buffer[bufidx] == '\0')
572 doBeep();
573 else {
574 int rc;
575 (void)rc; /* UNUSED */
576 int count = byteLengthOfCharacterAt(bufidx);
577 rc = write(1, (void *)&buffer[bufidx], count);
578 bufidx+=count;
579 }
580 }
581
582
583 static int byteLengthOfPreviousCharacter(int idx) {
584 int count = 1;
585 if (encodingOption == ENCODING_UTF) {
586 /* This is moving backwards over UTF-8 characters */
587 if (is_utf8_follow(buffer[idx-1]))
588 /* If top two bits are 10 then it's a UTF-8 follow-up byte, so backup till we find prefix */
589 while (is_utf8_follow(buffer[--idx]))
590 count++;
591 }
592 return count;
593 }
594
595
596
597 static void leftArrow(char ch)
598 {
599 if (bufidx == 0)
600 doBeep();
601 else {
602 bufidx -= byteLengthOfPreviousCharacter(bufidx);
603 moveCursorLeft(); /* And it's still just one character on the screen */
604 }
605 }
606
607
608 static void insertToggle(char ch)
609 {
610 int rc;
611 (void)rc; /* UNUSED */
612 rc = read(STDIN_FILENO, &ch, 1);
613 if (ch != 'z')
614 doBeep();
615 else
616 insertMode = !insertMode;
617 }
618
619
620 static void shiftBufferLeftFrom(int idx, int offset) {
621 for (int i = 0; i <= byte_length((char *)&buffer[idx+offset])+1; i++) {
622 buffer[idx+i] = buffer[idx+i+offset];
623 }
624 }
625
626
627 static void writeBufferFrom(int idx) {
628 int rc;
629 (void)rc; /* UNUSED */
630 rc = write(1, (void *)&buffer[idx], byte_length(&buffer[idx]));
631 }
632
633
634 static void moveCursorFromEndBackTo(int idx) {
635 for (int i = 0; i <= character_length((uchar *)&buffer[idx]); i++)
636 moveCursorLeft();
637 }
638
639
640
641 static void delBwd(char ch)
642 {
643 (void)ch; /* UNUSED - to match other keymap functions */
644
645 if (bufidx == 0)
646 doBeep();
647 else {
648 int deleted_length = 1;
649
650 commandLineChanged = true;
651
652 moveCursorLeft(); /* Move backwards over the deleted char */
653
654 deleted_length = byteLengthOfPreviousCharacter(bufidx);
655 bufidx -= deleted_length;
656
657 /* Move up any remaning characters in the buffer ... */
658 shiftBufferLeftFrom(bufidx, deleted_length);
659
660 /* ... on the screen, print the rest of the string ... */
661 writeBufferFrom(bufidx);
662 writeBlank(); /* And erase the character at the end still left on screen */
663
664 moveCursorFromEndBackTo(bufidx); /* Move back to current position */
665 }
666 }
667
668 static void delFwd(char ch)
669 {
670 if (bufidx > LINELENGTH || buffer[bufidx] == '\0')
671 doBeep();
672 else {
673 int i;
674
675 commandLineChanged = true;
676
677 int deleted_length = byteLengthOfCharacterAt(bufidx);
678 shiftBufferLeftFrom(bufidx, deleted_length);
679 writeBufferFrom(bufidx);
680 writeBlank();
681 for (i = 0; i <= character_length((uchar *)&buffer[bufidx]); i++)
682 moveCursorLeft();
683 }
684 }
685
686 static void escHook(char ch) {
687 int rc;
688 (void)rc; /* UNUSED */
689 rc = read(STDIN_FILENO, &ch, 1);
690 execute(escmap, ch);
691 }
692
693 static void arrowHook(char ch) {
694 int rc;
695 (void)rc; /* UNUSED */
696 rc = read(STDIN_FILENO, &ch, 1);
697 execute(arrowmap, ch);
698 }
699
700 static void newLine(char ch)
701 {
702 int rc;
703 (void)rc; /* UNUSED */
704
705 endOfInput = true;
706 rc = write(1, "\n", 1);
707
708 /* If the input is not the same as the previous, save it in the history */
709 if (commandLineChanged && strlen((char *)buffer) > 0) {
710 if (history[histidx] == NULL)
711 history[histidx] = (unsigned char *)allocate(LINELENGTH+1);
712 strcpy((char *)history[histidx], (char *)buffer);
713 histidx = (histidx+1)%HISTORYLENGTH;
714 }
715 }
716
717
718 static void shiftBufferRightFrom(int idx, int offset) {
719 for (int i = byte_length((char *)buffer); i >= idx; i--)
720 buffer[i+offset] = buffer[i];
721 buffer[byte_length(buffer)+1] = '\0';
722 }
723
724
725 static void moveCursorLeftTo(int idx) {
726 for (int i = character_length((uchar *)&buffer[idx]); i > 0; i--)
727 moveCursorLeft();
728 }
729
730
731 static void insertCh(uchar bytes[], int length) {
732 int rc;
733 (void)rc;
734
735 /* Make room for the bytes @bufidx */
736 shiftBufferRightFrom(bufidx, length);
737
738 /* Fill the buffer with the collected bytes */
739 for (int i=0; i < length; i++)
740 buffer[bufidx+i] = bytes[i];
741 writeBufferFrom(bufidx);
742
743 bufidx += length;
744 moveCursorLeftTo(bufidx);
745 }
746
747
748 static void overwriteCh(uchar bytes[], int length) {
749
750 int current_length = byteLengthOfCharacterAt(bufidx);
751
752 if (length < current_length)
753 shiftBufferLeftFrom(bufidx, current_length-length);
754 else
755 shiftBufferRightFrom(bufidx, length-current_length);
756
757 /* Fill the buffer with the collected bytes ... */
758 for (int i=0; i < length; i++) {
759 buffer[bufidx++] = bytes[i];
760 }
761
762 /* ... and write them */
763 int rc = write(1, bytes, length);
764 (void)rc;
765 }
766
767
768 static int utf8_follow_bytes(uchar ch) {
769 /* 110xxxxx -> 1 extra byte */
770 /* 1110xxxx -> 2 extra bytes */
771 /* 11110xxx -> 3 extra bytes */
772 if ((ch & 0xE0) == 0xC0)
773 return 2;
774 else if ((ch & 0xF0) == 0xE0)
775 return 3;
776 else if ((ch & 0xF8) == 0xF0)
777 return 4;
778 return -1;
779 }
780
781
782
783 static void normalCh(char ch) {
784 if (bufidx > LINELENGTH) {
785 doBeep();
786 return;
787 }
788 int rc;
789 (void)rc; /* UNUSED */
790 static uchar bytes[4];
791 static int length;
792 static int bytes_left;
793
794 if (encodingOption == ENCODING_UTF) {
795 if (is_utf8_prefix(ch)) {
796 /* Start collecting bytes in the array */
797 length = utf8_follow_bytes(ch);
798 bytes_left = length-1;
799 bytes[0] = ch;
800 return;
801 } else if (bytes_left > 0) {
802 bytes_left--;
803 bytes[length-1 - bytes_left] = ch;
804 if (bytes_left != 0)
805 return;
806 } else {
807 length = 1;
808 bytes[0] = ch;
809 }
810 } else {
811 length = 1;
812 bytes[0] = ch;
813 }
814
815 if (buffer[bufidx] == '\0' || insertMode) {
816 insertCh(bytes, length);
817 } else {
818 overwriteCh(bytes, length);
819 }
820
821 commandLineChanged = true;
822 }
823
824 #ifdef __win__
825 #include <windows.h>
826 #include <winbase.h>
827 #include <wincon.h>
828 #endif
829
830 /*----------------------------------------------------------------------*/
831 static void echoOff()
832 {
833 #ifdef HAVE_TERMIOS
834 newtermio();
835 #else
836 #ifdef __win__
837
838 HANDLE handle = GetStdHandle(STD_INPUT_HANDLE);
839
840 (void) SetConsoleMode(handle, 0);
841
842 #endif
843 #endif
844 }
845
846
847 /*----------------------------------------------------------------------*/
848 static void echoOn()
849 {
850 #ifdef HAVE_TERMIOS
851 restoretermio();
852 #else
853 #ifdef __win__
854
855 HANDLE handle = GetStdHandle(STD_INPUT_HANDLE);
856 (void) SetConsoleMode(handle, ENABLE_ECHO_INPUT);
857
858 #endif
859 #endif
860 }
861
862 #include <errno.h>
863
864 /*======================================================================
865
866 readline() - non-GLK case
867
868 Read a line from the user, with history, editing and command
869 reading from file
870
871 NOTE that the raw characters (and thus the maps) are in the
872 native/external encoding for the platform.
873
874 */
875
876 /* TODO - length of user buffer should be used */
877 bool readline(char usrbuf[])
878 {
879 static bool readingCommands = false;
880 static FILE *commandFile;
881 static int previousEncoding;
882 static bool firstInput = true;
883 static uchar BOM[3] = {0xEF,0xBB,0xBF};
884
885 if (readingCommands) {
886 fflush(stdout);
887 /* TODO: Arbitrarily using 255 for buffer size */
888 if (!fgets(buffer, 255, commandFile)) {
889 fclose(commandFile);
890 readingCommands = false;
891 encodingOption = previousEncoding;
892 buffer[0] = '\0';
893 goto endOfCommandFile;
894 } else {
895 printf("%s", buffer);
896 }
897 } else {
898 endOfCommandFile:
899 fflush(stdout);
900 bufidx = 0;
901 histp = histidx;
902 buffer[0] = '\0';
903 commandLineChanged = true;
904 echoOff();
905 //printf("ERROR = %s\n", strerror(errno));
906 endOfInput = false;
907 while (!endOfInput) {
908 int count = read(STDIN_FILENO, (void *)&ch, 1);
909 if (count == 0) {
910 /* We did not get any character at all? EOF? */
911 echoOn();
912 return false;
913 }
914 execute(keymap, ch);
915 }
916 echoOn();
917
918 if (buffer[0] == '@')
919 if ((commandFile = fopen(&buffer[1], "r")) != NULL)
920 if (fgets(buffer, 255, commandFile) != NULL) {
921 readingCommands = true;
922 if (memcmp(buffer, BOM, 3) == 0) {
923 previousEncoding = encodingOption;
924 encodingOption = ENCODING_UTF;
925 memmove(buffer, &buffer[3], strlen(buffer)-3+1);
926 }
927 printf("%s", buffer);
928 }
929 /* Reset line counter only if we read actual player input */
930 lin = 1;
931 }
932 stripNewline(buffer);
933 if (firstInput) {
934 firstInput = false;
935 if (memcmp(buffer, BOM, 3) == 0) {
936 encodingOption = ENCODING_UTF;
937 memmove(buffer, &buffer[3], strlen(buffer)-3+1);
938 }
939 }
940 char *converted = ensureInternalEncoding(buffer);
941 strcpy(usrbuf, converted);
942 free(converted);
943 return true;
944 }
945
946 #endif