Debian Codegraph @

terps/tads/tads2/qas.c

raw symbols SCIP
1 /*
2 * Copyright (c) 1991, 2002 Michael J. Roberts. All rights reserved.
3 *
4 * Please see the accompanying license file, LICENSE.TXT, for information
5 * on using and copying this software.
6 */
7 /*
8 Name
9 qas - qa scripter
10 Function
11 Allows TADS to read part or all of the commands from a session from a
12 file.
13 Notes
14 Some operating systems (e.g., Mac) obtain user input in ways that don't
15 involve the command line. For these systems to work properly, the os_xxx
16 routines that invoke other input methods must be "qa scripter aware"; for
17 example, the Mac os_askfile() routine must put the filename it gets back
18 in the command log file, or must read directly from the command log file,
19 or both.
20 Modified
21 03/10/91 MJRoberts - created
22 */
23
24 #include <stdio.h>
25 #include <string.h>
26 #include "os.h"
27 #include "run.h"
28 #include "tio.h"
29
30 /*
31 * Globals for the script reader
32 */
33 osfildef *scrfp = (osfildef *)0; /* script file */
34 int scrquiet = 0; /* flag: true ==> script is NOT shown as read */
35
36 /*
37 * open script file
38 */
39 int qasopn(char *scrnam, int quiet)
40 {
41 if (scrfp) return 1; /* already reading from script */
42 if ((scrfp = osfoprt(scrnam, OSFTCMD)) == 0) return 1;
43 scrquiet = quiet;
44 return 0;
45 }
46
47 /*
48 * close script file
49 */
50 void qasclose()
51 {
52 /* only close the script file if there's one open */
53 if (scrfp)
54 {
55 osfcls(scrfp);
56 scrfp = 0; /* no more script file */
57 scrquiet = 0;
58 }
59 }
60
61 /*
62 * Read the next line from the script file (this is essentially the
63 * script-redirected os_gets). Only lines starting with '>' are
64 * considered script input lines; all other lines are comments, and are
65 * ignored.
66 */
67 char *qasgets(char *buf, int bufl)
68 {
69 /* shouldn't be here at all if there's no script file */
70 if (scrfp == 0)
71 return 0;
72
73 /* update status line */
74 runstat();
75
76 /* keep going until we find something we like */
77 for (;;)
78 {
79 char c;
80
81 /*
82 * Read the next character of input. If it's not a newline,
83 * there's more on the same line, so read the rest and see what
84 * to do.
85 */
86 c = osfgetc(scrfp);
87 if (c != '\n' && c != '\r')
88 {
89 /* read the rest of the line */
90 if (!osfgets(buf, bufl, scrfp))
91 {
92 /* end of file: close the script and return eof */
93 qasclose();
94 return 0;
95 }
96
97 /* if the line started with '>', strip '\n' and return line */
98 if (c == '>')
99 {
100 int l;
101
102 /* remove the trailing newline */
103 if ((l = strlen(buf)) > 0
104 && (buf[l-1] == '\n' || buf[l-1] == '\r'))
105 buf[l-1] = 0;
106
107 /*
108 * if we're not in quiet mode, echo the command to the
109 * display
110 */
111 if (!scrquiet)
112 outformat(buf);
113
114 /* flush the current line without adding any blank lines */
115 outflushn(1);
116
117 /* return the command */
118 return buf;
119 }
120 }
121 else if (c == EOF )
122 {
123 /* end of file - close the script and return eof */
124 qasclose();
125 return 0;
126 }
127 }
128 }