This repository has been archived by the owner on Jul 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
scriplib.c
187 lines (151 loc) · 3.01 KB
/
scriplib.c
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// scriplib.c
#include "cmdlib.h"
#include "scriplib.h"
/*
=============================================================================
PARSING STUFF
=============================================================================
*/
char token[MAXTOKEN];
char *scriptbuffer,*script_p,*scriptend_p;
int grabbed;
int scriptline;
boolean endofscript;
boolean tokenready; // only true if UnGetToken was just called
/*
==============
=
= LoadScriptFile
=
==============
*/
void LoadScriptFile (char *filename)
{
long size;
size = LoadFile (filename, (void **)&scriptbuffer);
script_p = scriptbuffer;
scriptend_p = script_p + size;
scriptline = 1;
endofscript = false;
tokenready = false;
}
/*
==============
=
= UnGetToken
=
= Signals that the current token was not used, and should be reported
= for the next GetToken. Note that
GetToken (true);
UnGetToken ();
GetToken (false);
= could cross a line boundary.
=
==============
*/
void UnGetToken (void)
{
tokenready = true;
}
/*
==============
=
= GetToken
=
==============
*/
void GetToken (boolean crossline)
{
char *token_p;
if (tokenready) // is a token allready waiting?
{
tokenready = false;
return;
}
if (script_p >= scriptend_p)
{
if (!crossline)
Error ("Line %i is incomplete\n",scriptline);
endofscript = true;
return;
}
//
// skip space
//
skipspace:
while (*script_p <= 32)
{
if (script_p >= scriptend_p)
{
if (!crossline)
Error ("Line %i is incomplete\n",scriptline);
endofscript = true;
return;
}
if (*script_p++ == '\n')
{
if (!crossline)
Error ("Line %i is incomplete\n",scriptline);
scriptline++;
}
}
if (script_p >= scriptend_p)
{
if (!crossline)
Error ("Line %i is incomplete\n",scriptline);
endofscript = true;
return;
}
if (*script_p == ';' || *script_p == '#') // semicolon is comment field
{ // also make # a comment field
if (!crossline)
Error ("Line %i is incomplete\n",scriptline);
while (*script_p++ != '\n')
if (script_p >= scriptend_p)
{
endofscript = true;
return;
}
goto skipspace;
}
//
// copy token
//
token_p = token;
while ( *script_p > 32 && *script_p != ';')
{
*token_p++ = *script_p++;
if (script_p == scriptend_p)
break;
if (token_p == &token[MAXTOKEN])
Error ("Token too large on line %i\n",scriptline);
}
*token_p = 0;
}
/*
==============
=
= TokenAvailable
=
= Returns true if there is another token on the line
=
==============
*/
boolean TokenAvailable (void)
{
char *search_p;
search_p = script_p;
if (search_p >= scriptend_p)
return false;
while ( *search_p <= 32)
{
if (*search_p == '\n')
return false;
search_p++;
if (search_p == scriptend_p)
return false;
}
if (*search_p == ';')
return false;
return true;
}