1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "glk/tads/tads2/lib.h"
24 #include "glk/tads/tads2/run.h"
25 #include "glk/tads/tads2/vocabulary.h"
26 #include "glk/tads/os_frob_tads.h"
27 
28 namespace Glk {
29 namespace TADS {
30 namespace TADS2 {
31 
32 
33 /*
34  *  Globals for the script reader
35  */
36 osfildef *scrfp = (osfildef *)0;                             /* script file */
37 int scrquiet = 0;             /* flag: true ==> script is NOT shown as read */
38 
qasopn(char * scrnam,int quiet)39 int qasopn(char *scrnam, int quiet) {
40     if (scrfp) return 1;                     /* already reading from script */
41     if ((scrfp = osfoprt(scrnam, OSFTCMD)) == 0) return 1;
42     scrquiet = quiet;
43     return 0;
44 }
45 
qasclose()46 void qasclose() {
47     /* only close the script file if there's one open */
48     if (scrfp)
49     {
50         osfcls(scrfp);
51         scrfp = 0;                                   /* no more script file */
52         scrquiet = 0;
53     }
54 }
55 
qasgets(char * buf,int bufl)56 char *qasgets(char *buf, int bufl) {
57     /* shouldn't be here at all if there's no script file */
58     if (scrfp == 0)
59         return 0;
60 
61     /* update status line */
62     runstat();
63 
64     /* keep going until we find something we like */
65     for (;;)
66     {
67         char c;
68 
69         /*
70          *   Read the next character of input.  If it's not a newline,
71          *   there's more on the same line, so read the rest and see what
72          *   to do.
73          */
74         c = osfgetc(scrfp);
75         if (c != '\n' && c != '\r')
76         {
77             /* read the rest of the line */
78             if (!osfgets(buf, bufl, scrfp))
79             {
80                 /* end of file:  close the script and return eof */
81                 qasclose();
82                 return 0;
83             }
84 
85             /* if the line started with '>', strip '\n' and return line */
86             if (c == '>')
87             {
88                 int l;
89 
90                 /* remove the trailing newline */
91                 if ((l = strlen(buf)) > 0
92                     && (buf[l-1] == '\n' || buf[l-1] == '\r'))
93                     buf[l-1] = 0;
94 
95                 /*
96                  *   if we're not in quiet mode, echo the command to the
97                  *   display
98                  */
99                 if (!scrquiet)
100                     outformat(buf);
101 
102                 /* flush the current line without adding any blank lines */
103                 outflushn(1);
104 
105                 /* return the command */
106                 return buf;
107             }
108         } else if ((int)c == EOF) {
109             /* end of file - close the script and return eof */
110             qasclose();
111             return 0;
112         }
113     }
114 }
115 
116 } // End of namespace TADS2
117 } // End of namespace TADS
118 } // End of namespace Glk
119