aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/tads/tads2/get_string.cpp
blob: 624b8d95da9754fae735d68e7e1ea015f3921c71 (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
173
/* 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/tads/tads2/character_map.h"
#include "glk/tads/tads2/text_io.h"
#include "glk/tads/tads2/os.h"

namespace Glk {
namespace TADS {
namespace TADS2 {


/*
 *   Global variable with the current command logging file.  If this is
 *   not null, we'll log each command that we read to this file.  
 */
osfildef *cmdfile;

/*
 *   External global with the current script input file.  If this is
 *   non-null, we'll read commands from this file rather than from the
 *   keyboard.  
 */
extern osfildef *scrfp;

/*
 *   External global indicating script echo status.  If we're reading from
 *   a script input file (i.e., scrfp is non-null), and this variable is
 *   true, it indicates that we're in "quiet" mode reading the script, so
 *   we will not echo commands that we read from the script file to the
 *   display. 
 */
extern int scrquiet;

/*
 *   getstring reads a string from the keyboard, doing all necessary
 *   output flushing.  Prompting is to be done by the caller.  This
 *   routine should be called instead of os_gets.
 */
int getstring(const char *prompt, char *buf, int bufl)
{
    char  *result;
    int    savemoremode;
	int    retval = 0;

    /* show prompt if one was given and flush output */
    savemoremode = setmore(0);
    if (prompt != 0)
    {
        /* display the prompt text */
        outformat(prompt);

        /* make sure it shows up in the log file as well */
        out_logfile_print(prompt, FALSE);
    }
    outflushn(0);
    outreset();

    /* read from the command input file if we have one */
    if (scrfp != 0)
    {
        int quiet = scrquiet;
        
        /* try reading from command input file */
        if ((result = qasgets(buf, bufl)) == 0)
        {
            /*
             *   End of command input file; return to reading the
             *   keyboard.  If we didn't already show the prompt, show it
             *   now.
             *   
             *   Note that qasgets() will have closed the script file
             *   before returning eof, so we won't directly read the
             *   command here but instead handle it later when we check to
             *   see if we need to read from the keyboard.  
             */
            if (quiet && prompt != 0)
                outformat(prompt);
            outflushn(0);
            outreset();

            /*
             *   Guarantee that moremode is turned back on.  (moremode can
             *   be turned off for one of two reasons: we're printing the
             *   prompt, or we're reading from a script with no pauses.
             *   In either case, moremode should be turned back on at this
             *   point. -CDN) 
             */
            savemoremode = 1;

            /* turn off NONSTOP mode now that we're done with the script */
            os_nonstop_mode(FALSE);
        }

        /* success */
        retval = 0;
    }

    /* if we don't have a script file, read from the keyboard */
    if (scrfp == 0)
    {
        /* update the status line */
        runstat();
        
        /* read a line from the keyboard */
        result = (char *)os_gets((uchar *)buf, bufl);
        
        /* 
         *   if the result is null, we're at eof, so return a non-zero
         *   value; otherwise, we successfully read a command, so return
         *   zero 
         */
        retval = (result == 0);
    }

    /* restore the original "more" mode */
    setmore(savemoremode);

    /* check the result */
    if (retval != 0)
    {
        /* we got an error reading the command - return the error */
        return retval;
    }
    else
    {
        char *p;
        
        /* 
         *   we got a command, or at least a partial command (if we timed
         *   out, we may still have a partial line in the buffer) - write
         *   the input line to the log and/or command files, as
         *   appropriate 
         */
        out_logfile_print(buf, TRUE);
        if (cmdfile != 0)
        {
            os_fprintz(cmdfile, ">");
            os_fprintz(cmdfile, buf);
            os_fprintz(cmdfile, "\n");
        }

        /* translate the input to the internal character set */
        for (p = buf ; *p != '\0' ; ++p)
            *p = cmap_n2i(*p);

        /* success */
        return retval;
    }
}

} // End of namespace TADS2
} // End of namespace TADS
} // End of namespace Glk