aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/alan3/readline.cpp
blob: fa965bbd76a25a09cac17ccb6bc453a7b80ae09b (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* 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;
#ifdef HAVE_WINGLK
    static frefid_t logFileRef;
    INT_PTR e;
#endif

    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;
#ifdef HAVE_WINGLK
                case winglk_evtype_GuiInput:
                    switch (event.val1) {
                    case ID_MENU_RESTART:
                        restartGame();
                        break;
                    case ID_MENU_SAVE:
                        glk_set_style(style_Input);
                        printf("save\n");
                        glk_set_style(style_Normal);
                        save();
                        para();
                        printf("> ");
                        break;
                    case ID_MENU_RESTORE:
                        glk_set_style(style_Input);
                        printf("restore\n");
                        glk_set_style(style_Normal);
                        restore();
                        look();
                        para();
                        printf("> ");
                        break;
                    case ID_MENU_RECORD:
                        if (transcriptOption || logOption) {
                            glk_stream_close(logFile, NULL);
                            transcriptOption = FALSE;
                            logOption = FALSE;
                        }
                        logFileRef = glk_fileref_create_by_prompt(fileusage_InputRecord+fileusage_TextMode, filemode_Write, 0);
                        if (logFileRef == NULL) break;
                        logFile = glk_stream_open_file(logFileRef, filemode_Write, 0);
                        if (logFile != NULL)
                            logOption = TRUE;
                        break;
                    case ID_MENU_PLAYBACK:
                        commandFileRef = glk_fileref_create_by_prompt(fileusage_InputRecord+fileusage_TextMode, filemode_Read, 0);
                        if (commandFileRef == NULL) break;
                        commandFile = glk_stream_open_file(commandFileRef, filemode_Read, 0);
                        if (commandFile != NULL)
                            if (glk_get_line_stream(commandFile, buffer, 255) != 0) {
                                readingCommands = TRUE;
                                printf(buffer);
                                return TRUE;
                            }
                        break;
                    case ID_MENU_TRANSCRIPT:
                        if (transcriptOption || logOption) {
                            glk_stream_close(logFile, NULL);
                            transcriptOption = FALSE;
                            logOption = FALSE;
                        }
                        logFileRef = glk_fileref_create_by_prompt(fileusage_Transcript+fileusage_TextMode, filemode_Write, 0);
                        if (logFileRef == NULL) break;
                        logFile = glk_stream_open_file(logFileRef, filemode_Write, 0);
                        if (logFile != NULL) {
                            transcriptOption = TRUE;
                            glk_put_string_stream(logFile, "> ");
                        }
                        break;
                    case ID_MENU_ABOUT:
                        e = DialogBox(myInstance, MAKEINTRESOURCE(IDD_ABOUT), NULL, &AboutDialogProc);
                        (void)e;
                        break;
                    }
                    break;
#endif
				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