-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcTrace.pas
More file actions
498 lines (423 loc) · 13.1 KB
/
Copy pathProcTrace.pas
File metadata and controls
498 lines (423 loc) · 13.1 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
// Examine a file and trace every proc or func
{$I CrossrefPrefixCode.inc}
{$ifdef mswindows}{$apptype console}{$endif}
{$mode ObjFPC}{$H+}
program ProcTrace;
uses
windows, SysUtils;
Const
// Standard months of year
Months: array[1..12] of string[9]=
('January', 'February','March', 'April',
'May', 'June', 'July', 'August',
'September','October', 'November', 'December');
// Standard days of week
Days: Array[0..6] of string[9]=
('Sunday','Monday','Tuesday','Wednesday',
'Thursday','Friday','Saturday');
Type
{$IFDEF Bits64}
LargeInt = Int64;
{$ELSE}
{$IFDEF Bits32}
LargeInt = Integer;
{$ELSE}
{$PATAL Must define BITS64 or BITS32}
{$ENDIF}
{$ENDIF}
TracePtr = ^TraceTable;
TraceTable = record
Next: TracePtr; // ptr to next entry
ProcName: String; // identifier of p/f
InRecord: Boolean; // Inside of record (we ignore case stmt)
NestCount, // how many nested procs
BeginCount: Integer; // how many begin stmts
end;
VAR
Infile, //< Source file
Outfile: text; //< Output File
EndTS, //< Completion Time Stamp
TS: SystemTime; //< Start time stamp
TimeStamp: String; //< Text of time stamp
CH:WideChar; //< char we're looking at
InInterface, //< are we in interface of unit
ProcFlag: Boolean; //< true if we just saw a proc/func header
Ident, //< Identifier
Prefix, //< Part of line before insertion
Suffix, //< Part of line adter insertion
UCL: UnicodeString; //< Uppercase of text line
Trace: TracePtr;
FullName, //< Bame of input file
Backup, //< Name of backup file
Line: //< A line read from the input file
UnicodeString;
LineNumber, //< # of lines read
Index, //< currrent char in line
Len: Integer; //< Length of input line
IR: byte; //< IOResult save
// creates a text timestamp
Function CTS(Const CTime:SystemTime): AnsiString;
begin
Result := Days[CTime.dayOfWeek]+
' '+Months[CTime.month]+
' '+IntToStr(CTime.day)+
', '+IntToStr(CTime.year)+
' '+IntToStr(CTime.Hour)+
':';
if CTime.Minute < 10 then Result := Result+'0';
Result := Result+ IntToStr(CTime.Minute)+':';
if CTime.Second < 10 then Result := Result+'0';
Result := Result+ IntToStr(CTime.Second);
end;
Function Comma(K:LargeInt):string;
var
i:integer;
s: string;
begin
S := IntToStr(K);
i := length(s)-3;
while i>0 do
begin
S := Copy(S,1,i) +','+copy(s,i+1,length(s));
I := I-3;
end;
Result := S;
end;
Function Plural(N:Integer; Plu:String; Sng: String): string;
Var
s:String;
Begin
S := IntToStr(N);
S := ' '+S+' ';
If n<>1 Then
Result:= S+ Plu
Else
Result := S + Sng;
End; // Function Plural
Procedure Banner;
begin
Writeln('Proctrace - Examine Pascal source file for ',
'procedutes and functions and');
writeln('mark them for tracing');
writeln('Started: ',TimeStamp,', please wait...');
If (Paramcount <1) or (Paramcount >2) or
((Paramcount =2) and (UpperCase(Paramstr(1))<>'-D')) then
begin
writeln;
writeln('Usage:');
writeln('proctrace [-d] <file.pas>');
writeln;
writeln('optional param -d to remove tracecode');
writeln('(switch is not case sensitive)');
writeln;
write('Press enter to end program: ');
readln;
halt(99);
end;
end; // rocedure Banner
Procedure Elapsed(CONST StartTime,EndTime: SystemTime);
Var
H,M,S,MS: Integer;
TimeString: String;
Begin
// Now tell them how long it took
// Presumably this program did not run for days...
H := EndTime.Hour;
if StartTime.Hour < EndTime.Hour then
h:=H + 24;
h := h - StartTime.Hour;
M := EndTime.Minute ;
if M < StartTime.minute then
begin
H := H-1;
M := M+60;
end;
M := M - StartTime.minute;
S := EndTime.second ;
if S < StartTime.second then
BEGIN
M := M-1;
S := S+60;
END;
S := S-StartTime.second;
MS := EndTime.MilliSecond;
IF MS < StartTime.MilliSecond then
begin
MS := MS+1000;
S := S-1;
end;
MS := MS-StartTime.MilliSecond;
// we won't bother with days,
// nobody is going to process something taking that long (famous last words)
TimeString := ''; // Make sure it has nothing left over
If H >0 then
Timestring := Plural(H,'hours','hour')+' ';
If M >0 then
Timestring := TimeString + Plural(M,'minutes','minute')+' ';
if timestring <> '' then
Timestring := Timestring +' and ';
Timestring := TimeString + IntToStr(S)+'.' + IntToStr(MS)+' seconds.';
Writeln('Elapsed time: ',TimeString)
end; // Procedure Elapsed
// try to put file back if there was am error
procedure putback;
begin // try to put file back
close(infile);
if renameFile(backup,FullName) then // we did
Writeln('?Error ',IR,' Unable to read file "',
FullName,'" file skipped')
else // can't put it back
Writeln('Unable to restore file "',
FullName,'", renamed to ',Backup,
'" file skipped');
end; // procedure putback;
Procedure ReadLine;
begin
WriteLn(OutFile,Line);
if eof(Infile) then exit;
Readln(Infile, Line);
Inc(LineNumber);
len := Length(Line);
Index :=1;
UCL := UpperCase(Line);
end;
Procedure HandleProcName;
begin
if Trace = Nil then // it should have been set
begin
Writeln('Internal error 100: Trace Nil in Handleprocname');
Writeln;
writeln('Please report the above error.');
writeln('Program cannot continue - Press Enter to end program.: ');
readln;
Halt(100);
end;
Trace^.ProcName := Trace^.ProcName+Ident;
end;
Procedure HandleInterface;
Begin
InInterface := TRUE;
End;
Procedure HandleImplementation;
Begin
InInterface := FALSE;
End;
// procedure or funcrion header found
Procedure Procedural;
Var
TraceHold: Traceptr;
Begin
// Proc/func header in Interface is implidit forward
// so ignore that one
If not InInterface then
begin
ProcFlag := True;
TraceHold := Trace;
New(Trace);
Trace^.Next := TraceHold;
Trace^.BeginCount:= 0;
Trace^.NestCount:= 0;
Trace^.InRecord:= FALSE;
Trace^.ProcName:= Ident +' '; // save 'procedure' or 'function'
end;
End;
// forward or external mens this procedure or funcrion
// is not defined hertr, just declared, so ignore it
Procedure HandleForExt;
// we don't count this header
Var
TraceHold: Traceptr;
Begin
TraceHold := Trace;
iF Trace<>Nil then
begin
Trace := Trace^.Next;
Dispose(Tracehold);
end;
End;
Procedure HandleBegin;
Begin
End;
Procedure Structure;
Begin
End;
Procedure SpecialCase;
Begin
End;
Procedure HandleEnd;
Begin
End;
Procedure Comment(Const C:string;const Size:Integer);
begin
Inc(index);
if index>Len then
readLine;
repeat // find end of comment or end of line
CH := UCL[Index];
if (CH = C[1]) and (Size=1) then
begin
inc(index);
exit;
end;
if (CH=C[1]) and (Index<Len) and
(UCL[Index+1]=C[2]) then
begin
inc(index,2);
exit;
end;
Inc(Index);
until EOF(InFile);
end;
procedure ProcessFile;
var
NumArgs: byte;
DeleteMarks: boolean;
begin
NumArgs := ParamCount;
FullName := ParamStr(NumArgs); //< Get the original name
DeleteMarks := numArgs = 2;
Backup := FullName + '.bak'; //< Get the nackup name
// rename to Fullname + .bak, e.g pascal.pas.bak
if FileExists(Backup) then
// erase old backup
If not DeleteFile(Backup) then
// Error deleting previous backup
begin
Writeln('Unable to delete backup of "',
FullName,'"');
writeln;
write('Press enter to end program: ');
readln;
halt(2);
end;
// If we can't rename the file to the baxkup
// name, that's an error too
if not renameFile(FullName,backup) then
begin
// tell them cam't backup
Writeln('Unable to backup file "',
FullName,'" ');
writeln;
write('Press enter to end program: ');
readln;
halt(4);
end;
// Open the source for reading
Assign(Infile,Backup);
FileMode := 0; // open input file read only
{$I-} Reset(Infile); {$I+}
IR := IOResult; //< Check if error
if IR<>0 then //< sorry, error
begin // try to put it back
putback;
writeln;
write('Press enter to end program: ');
readln;
halt(10);
end;
// if we are here, start copying
Assign(Outfile,Fullname); // name replacement
{$I-} Rewrite(Outfile); {$I+} // create replacement
IR := IOResult;
if IR<>0 then // then there's an error
begin // try to put it back
putback;
write('Press enter to end program: ');
readln;
halt(20);
end;
while not eof(infile) do
begin
readline;
Index := 1;
repeat
CH := UCL[Index];
If (CH = '_') or
((CH>='A') and (CH<='Z')) then
begin // identifier
Ident := '';
while ((CH = '_') or
((CH>='A') and (CH<='Z'))) and
(Index<=Len) do
begin
Ident := Ident + CH;
Inc(Index);
If Index<=Len then CH := UCL[Index];
end;
if ProcFlag then
HandleProcName
else
CASE ident OF
'INTERFACE': HandleInterface;
'FORWARD', 'EXTERNAL': HandleForExt;
'IMPLEMENTATION': HandleImplementation;
'PROCEDURE','FUNCTION' : Procedural;
'BEGIN': HandleBegin;
'RECORD': Structure;
'CASE': SpecialCase;
'END': HandleEnd;
end // case
end
else if CH='''' then // quoted string
begin
// continue until end of string (or end of line)
// which we treat as end of string
while ((CH<>'''') and
(Index<=Len)) do
Inc(Index);
inc(Index); // either step over ' or go past end of line
end
else if CH ='{' then Comment('}',1) // Check for brace
else if CH ='(' then // check for block comment
begin
If Index<Len then
If UCL[Index+1]='*' then
begin
Inc(Index); // step over *
Comment('*)',2)
end
end
else if CH ='/' then // check line or special block
begin // in case a line ends with . such as if a
// formula is broken over two lines
If Index<Len then
// stop if // found
If UCL[Index+1]='/' then
Index := Len // skip rest of line
else if UCL[Index+1]='*' then
begin
Inc(Index); // step over *
Comment('*/',2) // if C-style comments allowed
end
end
else If (CH = '$') or
((CH>='0') and (CH<='9')) then
begin // handle numbers
Inc(Index);
CH := UCL[Index];
while (((CH>='0') and (CH<='9')) or
((CH>='A') and (CH<='F')) or // Hex
(CH='+') or (CH='-') or // sign or decimal point
(CH='.')) and (Index<=Len) do
Inc(Index);
end;
inc(Index);
until Index>Len;
Readline;
end;// while not eof
end;
begin // .main.
GetLocalTime(TS);
TimeStamp := CTS(TS);
Banner;
Trace := NIL;
ProcessFile;
Close(OutFile);
Close(InFile);
writeln;
GetLocalTime(EndTS);
TimeStamp := CTS(EndTS);
Writeln('Completed ',TimeStamp);
Elapsed(TS,EndTS);
writeln;
end.