aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/alan3/readline.cpp
blob: 2bd408f7cbcac52196b542e7e535d62faf28c52d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "glk/alan3/readline.h"

#include "glk/alan3/sysdep.h"
#include "glk/alan3/output.h"
#include "glk/alan3/term.h"
#include "glk/alan3/exe.h"
#include "glk/alan3/save.h"
#include "glk/alan3/location.h"

#include "glk/alan3/options.h"
#include "glk/alan3/alan3.h"
#include "glk/alan3/glkio.h"
#include "glk/alan3/resources.h"

namespace Glk {
namespace Alan3 {

#define LINELENGTH 1000

/*======================================================================

  readline()

  Read a line from the user, with history and editing

*/

/* TODO - length of user buffer should be used */
bool readline(char buffer[]) {
	event_t event;
	static bool readingCommands = FALSE;
	static frefid_t commandFileRef;
	static strid_t commandFile;

	if (readingCommands) {
		if (g_vm->glk_get_line_stream(commandFile, buffer, 255) == 0) {
			g_vm->glk_stream_close(commandFile, NULL);
			readingCommands = FALSE;
		} else {
			g_vm->glk_set_style(style_Input);
			printf(buffer);
			g_vm->glk_set_style(style_Normal);
		}
	} else {
		g_vm->glk_request_line_event(glkMainWin, buffer, 255, 0);
		/* FIXME: buffer size should be infallible: all existing calls use 256 or
		   80 character buffers, except parse which uses LISTLEN (currently 100)
		*/
		do {
			g_vm->glk_select(&event);
			switch (event.type) {
			case evtype_Arrange:
				statusline();
				break;

			default:
				break;
			}
		} while (event.type != evtype_LineInput);
		if (buffer[0] == '@') {
			buffer[event.val1] = 0;
			commandFileRef = g_vm->glk_fileref_create_by_name(fileusage_InputRecord + fileusage_TextMode, &buffer[1], 0);
			commandFile = g_vm->glk_stream_open_file(commandFileRef, filemode_Read, 0);
			if (commandFile != NULL)
				if (g_vm->glk_get_line_stream(commandFile, buffer, 255) != 0) {
					readingCommands = TRUE;
					g_vm->glk_set_style(style_Input);
					printf(buffer);
					g_vm->glk_set_style(style_Normal);
				}
		} else
			buffer[event.val1] = 0;
	}
	return TRUE;
}

} // End of namespace Alan3
} // End of namespace Glk