forked from jrsoftware/issrc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIsppPreprocess.pas
306 lines (275 loc) · 8.24 KB
/
IsppPreprocess.pas
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
{
Inno Setup Preprocessor
Copyright (C) 2001-2002 Alex Yackimoff
Inno Setup
Copyright (C) 1997-2020 Jordan Russell
Portions by Martijn Laan
For conditions of distribution and use, see LICENSE.TXT.
}
unit IsppPreprocess;
interface
uses
CompPreprocInt;
function ISPreprocessScript(var Params: TPreprocessScriptParams): Integer; stdcall;
implementation
uses
SysUtils, CmnFunc2, PathFunc,
IsppBase, IsppPreprocessor, IsppSessions, IsppIntf, IsppIdentMan, IsppVarUtils, IsppConsts;
procedure ReadScript(const Params: TPreprocessScriptParams;
const Preprocessor: TPreprocessor);
var
I: Integer;
LineText: PChar;
LineTextStr: String;
begin
I := 0;
while True do
begin
LineText := Params.LineInProc(Params.CompilerData, 0, I);
if LineText = nil then
Break;
LineTextStr := LineText;
Preprocessor.QueueLine(LineTextStr);
Inc(I);
end;
end;
function CleanupProc(CleanupProcData: Pointer): Integer; stdcall;
begin
if PopPreproc = nil then
Result := 0
else
Result := 1; { should never get here }
end;
function DecodeStringOptions(const S: String; var Options: TOptions): Boolean;
var
I: Integer;
begin
Options := [];
Result := True;
for I := 1 to Length(S) do begin
case S[I] of
'a'..'z': Include(Options, Ord(S[I]) - Ord('a'));
else
Result := False;
end;
end;
end;
function ISPreprocessScript(var Params: TPreprocessScriptParams): Integer; stdcall;
const
Delta = Ord('a');
DefaultOptions: TIsppOptions =
(ParserOptions: (Options: [Ord('b') - Delta, Ord('p') - Delta]);
Options: [Ord('c') - Delta, Ord('e') - Delta]; VerboseLevel: 0;
InlineStart: '{#'; InlineEnd: '}'; SpanSymbol: #0);
var
ISPPOptions: TIsppOptions;
Definitions, IncludePath, IncludeFiles: String;
Preprocessor: TPreprocessor;
V: TIsppVariant;
function ParseOption(const OptName, OptValue: String): Boolean;
begin
Result := True;
if OptName = 'ISPP:ParserOptions' then
Result := DecodeStringOptions(OptValue, ISPPOptions.ParserOptions.Options)
else if OptName = 'ISPP:Options' then
Result := DecodeStringOptions(OptValue, ISPPOptions.Options)
else if OptName = 'ISPP:VerboseLevel' then
ISPPOptions.VerboseLevel := StrToIntDef(OptValue, 0)
else if OptName = 'ISPP:InlineStart' then
ISPPOptions.InlineStart := AnsiString(OptValue)
else if OptName = 'ISPP:InlineEnd' then
ISPPOptions.InlineEnd := AnsiString(OptValue)
else if OptName = 'ISPP:Definitions' then
Definitions := OptValue
else if OptName = 'ISPP:IncludePath' then
IncludePath := OptValue
else if OptName = 'ISPP:IncludeFiles' then
IncludeFiles := OptValue
else
Result := False;
end;
function ParseOptions(P: PChar): Boolean;
var
EqPos: PChar;
OptName: String;
begin
Result := True;
if P = nil then
Exit;
while P^ <> #0 do begin
EqPos := StrScan(P, '=');
if EqPos = nil then begin
Result := False;
Break;
end;
SetString(OptName, P, EqPos - P);
P := EqPos + 1;
if not ParseOption(OptName, P) then begin
Result := False;
Break;
end;
Inc(P, StrLen(P) + 1);
end;
end;
function ParseDefinitions(Definitions: PChar; VarMan: TIdentManager): Boolean;
procedure ParseDefinition(const S: string);
var
I: Integer;
Name, V: string;
Value: TIsppVariant;
begin
Value := NULL;
I := Pos('=', S);
if I > 0 then
begin
Name := Copy(S, 1, I - 1);
V := Copy(S, I + 1, MaxInt);
if V <> '' then
MakeStr(Value, V)
end
else
Name := Trim(S);
VarMan.DefineVariable(Name, -1, Value, dsPublic);
end;
var
DelimPos: PChar;
N: Integer;
Definition: string;
begin
Result := True;
while Definitions^ <> #0 do begin
DelimPos := StrScan(Definitions, #1);
if DelimPos = nil then begin
Result := False;
Break;
end;
N := DelimPos - Definitions;
if N > 0 then begin
SetString(Definition, Definitions, N);
ParseDefinition(Definition);
end;
Inc(Definitions, N + 1);
end;
end;
function IncludeBuiltinsAndParseIncludeFiles(BuiltinsDir: String; IncludeFiles: PChar; Options: TOptions): Boolean;
function Escape(const S: string): string;
var
I: Integer;
begin
Result := '';
for I := 1 to Length(S) do
begin
Result := Result + S[I];
if S[I] = '\' then Result := Result + '\';
end;
end;
procedure Include(FileName: String; Builtins: Boolean);
begin
if not GetOption(Options, 'P') then
FileName := Escape(FileName);
Preprocessor.IncludeFile(FileName, Builtins, False, True);
end;
const
SBuiltins = 'ISPPBuiltins.iss';
var
DelimPos: PChar;
N: Integer;
IncludeFile: String;
begin
Result := True;
IncludeFile := BuiltinsDir + SBuiltins;
if FileExists(IncludeFile) then
Include(IncludeFile, True)
else
Preprocessor.WarningMsg(SFileNotFound, [SBuiltins]);
while IncludeFiles^ <> #0 do begin
DelimPos := StrScan(IncludeFiles, #1);
if DelimPos = nil then begin
Result := False;
Break;
end;
N := DelimPos - IncludeFiles;
if N > 0 then begin
SetString(IncludeFile, IncludeFiles, N);
Include(IncludeFile, False);
end;
Inc(IncludeFiles, N + 1);
end;
end;
var
SourcePath, CompilerPath, LineFilename, LineText: string;
LineNumber: Integer;
begin
if (Params.Size <> SizeOf(Params)) or
(Params.InterfaceVersion <> 2) then
begin
Result := ispeInvalidParam;
Exit;
end;
SourcePath := Params.SourcePath;
CompilerPath := Params.CompilerPath;
ISPPOptions := DefaultOptions;
Definitions := '';
IncludePath := RemoveBackslashUnlessRoot(CompilerPath);
IncludeFiles := '';
if not ParseOptions(Params.Options) then
begin
Result := ispeInvalidParam;
Exit;
end;
{ Hack: push a dummy item onto the stack to defer deletion of temp. files }
PushPreproc(nil);
try
Preprocessor := TPreprocessor.Create(Params, nil, ISPPOptions, SourcePath,
CompilerPath, Params.Filename);
try
Preprocessor.IncludePath := IncludePath;
MakeStr(V, SourcePath);
Preprocessor.VarMan.DefineVariable('SourcePath', -1, V, dsPublic);
MakeStr(V, CompilerPath);
Preprocessor.VarMan.DefineVariable('CompilerPath', -1, V, dsPublic);
MakeInt(V, Params.CompilerBinVersion);
Preprocessor.VarMan.DefineVariable('Ver', -1, V, dsPublic);
if not ParseDefinitions(PChar(Definitions), Preprocessor.VarMan) or
not IncludeBuiltinsAndParseIncludeFiles(Params.CompilerPath, PChar(IncludeFiles),
Preprocessor.FOptions.ParserOptions.Options) then
begin
Result := ispeInvalidParam;
Exit;
end;
ReadScript(Params, Preprocessor);
Preprocessor.Stack.Resolved;
if not GetOption(Preprocessor.FOptions.Options, 'C') then
Result := ispeSilentAbort
else
begin
Preprocessor.GetNextOutputLineReset;
while Preprocessor.GetNextOutputLine(LineFilename, LineNumber, LineText) do
Params.LineOutProc(Params.CompilerData, PChar(LineFilename),
LineNumber, PChar(LineText));
Result := ispeSuccess;
end;
finally
Preprocessor.Free;
end;
except
on E: EPreprocError do {preprocessor (syntax most likely) error}
begin
Params.ErrorProc(Params.CompilerData, PChar(E.Message),
PChar(E.FileName), E.LineNumber, E.ColumnNumber);
Result := ispePreprocessError;
end;
on E: Exception do
begin
Params.ErrorProc(Params.CompilerData,
PChar(Format('Unexpected exception of class %s in ISPP.' +
#13#10#13#10'%s.', [E.ClassName, E.Message])), nil, 0, 0);
Result := ispePreprocessError;
end;
end;
if Result = ispeSuccess then
Params.PreprocCleanupProc := CleanupProc
else
PopPreproc;
end;
end.